LARAVEL update error Call to a member function update() on null - sql-update

$update = TicketDetails::find($ticket_id)
->update([
'redirected' => '1',
'department_id' => $department_id
]);
Doesn't work. Whats the problem?

I resolve the problem. The $ticket_id variable was the null.

Related

Rails how to access a hash in model from console

Using Rails 4. I have a hash in a model and I'd like to be able to access it via the rails console for testing some code. How do I do this?
foo = {'one' => 'ONE', 'two' => 'TWO'}
I've tried the following, but it gives me an "undefined method 'foo' in class..."
ModelName.foo['one']
Thanks for helping.
Are you trying to use foo as a constant? If so, you can set it and then reference it as:
class ModelName
FOO = {'one' => 'ONE', 'two' => 'TWO'}
end
ModelName::FOO['one']
An alternative to keep the reference as you have it:
class ModelName
def self.foo
{'one' => 'ONE', 'two' => 'TWO'}
end
end

Yii2 Cookie not generating

I am trying to set the cookie but cookie is not getting saved. Below is what I have tried:
$cookies = Yii::$app->response->cookies;
$cookies->add(new \yii\web\Cookie([
'name' => 'abc',
'value' => 'xyz',
'expire' => time() + 86400 * 365,
]));
$cookies1 = Yii::$app->request->cookies;
if ($cookies1->has('abc'))
$cookieValue = $cookies1->getValue('abc');
echo 'value : '.$cookieValue;
echo '<pre>'; print_r($_COOKIE);
$cookieValue does not hold any value. Cookie isn't generated. What am I doing wrong?
Your code is fine. Your problem is that you are trying to set and then get the cookie in the same request.
Your browser has not yet received the response, so it has not had the chance to add the cookie before you try to read it out.
You just need to set and then fetch the cookie in separate requests:
public function actionSetCookie() {
$cookies = Yii::$app->response->cookies;
$cookies->add(new \yii\web\Cookie([
'name' => 'abc',
'value' => 'xyz',
'expire' => time() + 86400 * 365,
]));
echo 'Cookie set!';
}
public function actionGetCookie() {
$cookies1 = Yii::$app->request->cookies;
if ($cookies1->has('abc'))
$cookieValue = $cookies1->getValue('abc');
echo 'value : '.$cookieValue;
}
Set your cookie like this
$cookie = Yii::$app->response->cookies;
$cookie = new \yii\web\Cookie
([
'name' => 'abc',
'value' => 'xyz',
'expire' => time() + 86400 * 365,
]);
Yii::$app->getResponse()->getCookies()->add($cookie);
//check cookie is exist or not
if(Yii::$app->getRequest()->getCookies()->has('abc'))
{
// if exist then get cookie value
$username = Yii::$app->getRequest()->getCookies()->getValue('abc');
}
Just putting here my answer, as several time visited this question but could not find solution. I spent one whole day to solve it. So hope this answer will help someone.
In my case I've used axios package which sent request from frontend and I got response Set-Cookie in the header but not saved in the browser. So setting axios.defaults.withCredentials = true; solved my issue.

CakePHP: calling testAction to a json-returning method causes missing view exception

What am I missing here? Here is my controller code:
public function calculate() {
$this->set(array(
"route" => array("A" => 1, "B" => 2),
"_serialize" => array("route")
));
return;
}
Here is a line from my routes.php file:
Router::parseExtensions();
Here is my test code:
$result = $this->testAction("/itinerary/calculate.json", array(
"method" => "POST",
"return" => "contents"
));
This code throws
MissingViewException: View file "C:\xampp\htdocs\fiver\app\View\Itinerary\calculate.ctp" is missing.
I am obviously missing something here. Please help. Another test for another controller with JSON works just fine
Got it. CakePHP requires the RequestHandler component to be explicitly added to the controller for the extensions to work. I've added this line, it started to work
public $components = array('RequestHandler');
If you dont have/want a view for your controller you can simply add
$this->autoRender = false;
// EDIT: Only working if you dont want an output but thats not the case

Silex and Doctrine - ORMException: Unknown Entity namespace alias [semi fixed]

I'm trying to use the Doctrine components in my app built using silex. I was able to get it to work - well almost.
I have my "User" entity and the corresponding repository
When doing
$app['em']->getRepository('Foo\Entity\User')->findAll()
works as expected, however when trying to make a custom query
$this->getEntityManager()
->createQuery(
'SELECT
u
FROM
Foo:User u
WHERE c.id = :x'
)
->setParameter('x',$in)
->getResult();
I get this exception
ORMException: Unknown Entity namespace alias 'Foo'
Please ignore the fact that I can make a select with findById() or findBy(array('id'=>$in)) the problem is with the custom query
This are my configs
$app['orm.em.options'] = array(
'mappings' => array(
array(
'type' => 'annotation',
'namespace' => 'Foo\Entity',
'alias' => 'core',
'path' => '%app.path%/src/Foo/Entity',
'use_simple_annotation_reader' => false,
)
));
and
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src/Foo/Entity"));
$params = $app['db.options'];
$app['em'] = EntityManager::create($params, $config);
After some research possible solutions:
auto_mapping: true => tried, no success
registering the namespace => tried, not sure if right was done so may be the solution, please advice how to do it right
besides all this I have tried to search for git repos with similar 'usage' but didn't get it :(
UPDATE
for the moment I use the following line in my query and it works
FROM
InstaLikes\Entity\User u
When you create custom queries, you should use the fully namespace, in this case:
Foo\Entity\User
I am assuming you have checked the alias you have given in the mappings options?
$app['orm.em.options'] = array(
'mappings' => array(
array(
'type' => 'annotation',
'namespace' => 'Foo\Entity',
'alias' => 'core',
'path' => '%app.path%/src/Foo/Entity',
'use_simple_annotation_reader' => false,
)
));
Should that alias option not be set to Foo instead?

CakePHP Unit Testing Mock Controller

I am trying to test that a value is entered into the session component with CakePHP Mock objects, but my test keeps failing stating that the write method has not been called. I have debugged this and know for a fact the method has been called, so not sure what I am doing wrong.
Here is the code in the unit test:
$this->controller = $this->generate('Posts', array(
'components' => array(
'Session' => array('write')
)));
$this->testAction(...);
$this->controller->Session
->expects($this->any())
->method('write')
->with('my value here');
I get the following error:
Expectation failed for method name is equal to when invoked zero or more times.
Mocked method does not exist.
If I change the call to expects to be ->expects($this->once()) I then get this error:
Expectation failed for method name is equal to when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.
I did a var_dump on $this->Controller, and there is definitely a mocked session object, and it does appear to notice the call to the 'write' method, so I'm really not sure why I'm getting the error message.
Any advice would be appreciated!
The mock set-up code should be before the call to $this->testAction(), as in:
$this->controller = $this->generate('Posts', array(
'components' => array(
'Session' => array('write')
)));
//Add the method mock details here
$this->controller->Session
->expects($this->any())
->method('write')
->with('my value here');
//Then call testAction
$this->testAction(...);
That should solve your problem.