wwwabcd
(Www Abcd)
December 22, 2014, 7:15pm
1
I have table Post and in this table I have one field that used serialize.
I want get all data from table and then in the view use unserialize for this field but in $models this field is NULL. How can get this field with findAll or another method?
$models = Post::model()->findAll('status = :status',array('status'=>2,));
oligalma
(Marc Oliveras)
December 22, 2014, 8:09pm
2
$models = Post::model()->findAll('status = ?',array(2));
or
$models = Post::model()->findAll(array('condition' => 'status =:status', 'params' => array(':status' => 2)));
alirz23
(Alirz23)
December 22, 2014, 9:48pm
3
okay another option
$models = Post::model()->findAllByAttributes(array('status' => 2));
oligalma
(Marc Oliveras)
December 23, 2014, 12:13am
5
which is better, findAll or findAllByAttributes?
alirz23
(Alirz23)
December 23, 2014, 12:38am
6
findAllByAttributes is just syntactic sugar, under the hood both compile down to the same method call
oligalma
(Marc Oliveras)
December 23, 2014, 1:40am
7
here says that findAllByAttributes protects against sqlinjection, is that true?
http://www.yiiframework.com/wiki/275/how-to-write-secure-yii-applications/#hh11
wwwabcd
(Www Abcd)
December 23, 2014, 11:06am
8
I have another question.
Table Post relation with table2 and table2 relation with table3
In Post, I need get data from table3.
In Post model I wrote this relation:
'table2'=>array(self::BELONGS_TO, 'Table2', 'w_id')
In with(array()) I just add table2. How can get data from table3?
I try use join instead with(array()) but I don’t understand why join didn’t work.
Then I used query command and I just get data from Post and table1:
$model = Yii::app()->db->createCommand()
->select('t.*, t2.*,t3.*')
->from('post t')
->leftJoin('table2 t2', 't.w_id = t2.id')
->leftJoin('table3 t3', 't2.y = t3.id')
->where('status =:status', array(':status'=>2))
->queryRow();
How can join 3 tables?
if Post is related to table2 and table2 is related to table2, than:
U already have relation from Post to table2 (‘table2’),
make relation from table2 to table 3 (for example ‘table3’)
Than once U get Post recordes U need U can just go trough relations:
$post->table2->table3 to get table3 data
wwwabcd
(Www Abcd)
December 23, 2014, 11:48am
10
Please write this
$model = Yii::app()->db->createCommand()
->select('t.*, t2.*,t3.*')
->from('post t')
->leftJoin('table2 t2', 't.w_id = t2.id')
->leftJoin('table3 t3', 't2.y = t3.id')
->where('status =:status', array(':status'=>2))
->queryRow();
with find(array()) and use with(array())
$models = Post::model()->find(array('with'=>array('')))
wwwabcd
(Www Abcd)
December 23, 2014, 3:07pm
11
This is solution:
In model for table2:
'VarName3'=>array(self::BELONGS_TO, 'VarName3', 'y'),
In Post model
'VarName2'=>array(self::BELONGS_TO, 'table2', 'w_id','with'=>'VarName3'),
Controler:
$model = Post::model()->with()->find(array(
'condition'=>'status =:status',
'with'=>array('VarName2'),
'params' =>array(
':status' => 2,
),
));
alirz23
(Alirz23)
December 24, 2014, 3:56pm
12
how about this
$model = Post::model()->with('VarName2')->findByAttributes(array('status' => 2));