i have a very weird thing going on here. I check in the AppController's beforeFilter() function, wether an id exists or not. It works when I open the url /project/user/1, but when I open /project/user/edit/1 it says that I use a function on a non-object. I already checked if I really do so, but I don't. I output the value with pr() and it shows me the object content.
Here is my beforeFilter() function:
$request = $this->request;
if (isset($request->params['id'])) {
$model = Inflector::singularize(ucfirst($request->params['controller']));
$cond = array('conditions' => array($model . '.id' => $request->params['id']));
pr($this->{$model});
exit;
if (!$this->{$model}->find('first', $cond)) {
$this->Session->setFlash(__('Invalid id.'), 'flash_notice');
$this->redirect(array(
'manager' => (bool)$request->params['manager'],
'controller' => 'tournaments',
'action' => 'index'
));
}
}
The output of $model, before the id condition gets executed, contains the object data and is absolutley correct. I don't see the problem :/
Can you help me?
Thanks.
Okay I got the error...
The code just simply doesn't work, when the URL you open has some other error for example a database connection error. It's hard for me to explain but when I removed the code above and opened my URL an error like this came:
Database Error
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You
have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'isAdmin'
at line 1
When I fixed that bug, the code above works fine.
Related
I have tried to get data from database with laravel Where JSON. But it gives an error. I have added the data like this.
$event = new Event;
$event->scores = json_encode([['user_ids' => [1,2,3,4],'score' => 15]]);
$event->save();
When I want to return the data in database.
Event::where('scores->score',15)->get()
Shows this error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your
MariaDB server version for the right syntax to use near '>'$."score"'
= ?' at line 1 (SQL: select * from `events` where `scores`->'$."score"' = 15)
My MariaDB version is 10.2.1
Array & JSON Casting
On your Event model you need to add
class Event extends Model {
protected $casts = ['scores'=>'array'];
// ...
}
and then when saving the data
$event = new Event;
// you had what appeared to be an extra array, that could cause the issue?
$event->scores = ['user_ids' => [1,2,3,4],'score' => 15];
$event->save();
Then the data will be saved as JSON automatically as long as the column type is TEXT or JSON(new versions of MySQL).
See Array & JSON casting on the Laravel Documentation: https://laravel.com/docs/5.5/eloquent-mutators#array-and-json-casting
Then to retrieve:
Event::where('scores->score', '15')->get();
Here is the relevant documentation for JSON Where clauses
https://laravel.com/docs/5.5/queries#json-where-clauses
Again, pretty confused as to what you are asking. Might help to see structure of the table or what the expected SQL should look like.
If you are trying to return rows based off values contained inside a string of JSON stored on the table... Then you need to do something like...
->whereRaw("JSON_CONTAINS(score, '[15]' )")->get();
Or something along those lines... https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html
I'm having a problem implementing an automatic login based on the NT user received in the http headers. (intranet website)
I've added the following code in the beforeAction of my yii\base\Controller.php
public function beforeAction($action)
{
if (!Yii::$app->user->identity) {
Yii::$app->user->enableSession = false;
//the NT username received in the HTTP headers
$ntnet = \app\models\Users::getNTNET();
//var_dump($ntnet);
$model = \app\models\Users::findOne(["LOWER(`user_name`)" => strtolower($ntnet)]);
if (!is_null($model))
Yii::$app->user->login($model);
}
$event = new ActionEvent($action);
$this->trigger(self::EVENT_BEFORE_ACTION, $event);
return $event->isValid;
If I uncomment the var_dump($ntnet) line, it will work, and echo the name, if not, $ntnet will be null, same goes for the $model, if I var dump it (as well as $ntnet before that), it will output the entire user properties and login successfully, otherwise, it will be null, why is it? it makes no sense to me.
For instance - I've intentionally added a typo in the "LOWER()" function in to receive the SQL error and view the entire query being executed, and if var_dump($ntnet) is comment, it will produce the following,
As you can see the $ntnet parameter is empty:
SQLSTATE[42000]: Syntax error or access violation: 1305 FUNCTION oncall.LOWaER does not exist
The SQL being executed was: SELECT * FROM `users` WHERE LOWaER(`user_name`)=''
With var_dump($ntnet) the error will be as following:
SQLSTATE[42000]: Syntax error or access violation: 1305 FUNCTION oncall.LOWaER does not exist
The SQL being executed was: SELECT * FROM `users` WHERE LOWaER(`user_name`)='myusername'
Thanks in advance.
Pls check implementation __debugInfo() method in User class or extendet classes, and add to question description and User::getNTNET() to.
To get username and password from HTTP header,
Use
$username = \Yii::$app->request->authUser;
and $password = \Yii::$app->request->authPassword;
Also check the spelling of lower() function. On your trace message, it's LOWaER. It should be LOWER
i have my controller function like this:
private function _send_refund_scores($userId,$scores,$orderId){
$this->loadModel('Score');
$rtn = $this->Score->refund_user_scores($userId,$scores,$orderId);
if(!empty($rtn)){
return true;
}else return false;
}
while in another model Score , I have model function refund_user_scores;
the error like this:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near
'refund_user_scores' at line 1
so,any idea?
I guess you have defined refund_user_scores() as a private function in model Score.
That is why you are getting this error.
I'm using Yii PHP Framework and MemCache for caching and when I open not cached page for the first time I see this error:
Live error error log off (linux):
Fatal error: Call to a member function getColumnNames() on a non-object in /home/cineshell/cineklik/framework/db/ar/CActiveFinder.php on line 385
Locally with error log (windows):
Trying to get property of non-object
C:\xampp\htdocs\appfolder\framework\yiilite.php(7125)
...
7123 public function getTableSchema()
7124 {
7125 return $this->getMetaData()->tableSchema;
7126 }
config/main:
'cache'=> array(
'class'=> 'CMemCache',
'servers'=>array(
array('host'=>'localhost', 'port'=>11211, 'weight'=>100),
),
),
Cache example:
$data = Yii::app()->cache->get( 'casheID' );
if($data===false)
{
$data = dataprovider, query... etc
}
echo $data;
So, what's error here ?, and could be something in server settings ?
someone told me use if isset but I'm not understand !
For CActiveDataProvider, please follow this guide.
http://www.yiiframework.com/wiki/233/using-cache-in-cactivedataprovider/
For alternative, you can use fragment cache the gridview or the partial view depending the needs. I've also struggeled with it and I found that query cache was the only working solution.
I've got a custom database class which extends the MySQLi class.
It connects to the database using parent in the __construct method.
Below is the portion of the query if the query is not successful, how do I return the error from the server?
$query = parent::query($querystr, $resultmode);
if (!$query) {
$error = str_replace(
'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use',
'Syntax Error',
mysqli_error(self::getInstance()));
\core\debug::sqlerrorlog($error);
} else {
\core\debug::sqlSuccess();
}
According to docs, you just need to do $this->error.
As I understand, your problem now is that you don't pass a correct intsance of mysqli to mysql_error - maybe self::getInstance() is not doing what it supposed to, but I can't tell from what I see.