I think database operations isn't explained very well, on guide . I couldn't understand it. Because of this i have a question. I asked it Yii Forums but there isn't any answer. This is my socials table for example.
+------------+---------------------------+--------------+--------------+---------------+
| socials_ID | socials_link | socials_type | socials_user | socials_order |
+------------+---------------------------+--------------+--------------+---------------+
| 48 | link | 8 | 1 | 4 |
| 47 | blablabla | 11 | 1 | 3 |
| 301 | userlinkuse | 9 | 1 | 6 |
+------------+---------------------------+--------------+--------------+---------------+
I want to get all datas from this table which socials_user collumn equals to 1 . There can be a few rows (in this example there are 3 rows) .
What method should i use ? I'm trying this :
$allSocial = '';
$socials=Socials::model()->findByAttributes(array('socials_user'=>1));
foreach ($socials as $social)
{
$type = $social["socials_type"];
$allSocial .= $type . ",";
}
return $allSocial;
but this is returning 4 , l , 8 ,1 , 4 . (First letter / number of every c开发者_开发技巧ollumn on first row)
How can i use it ? findByAttributes AR is adding LIMIT 1;
to SQL ?
Use this method to get records from the table having socials_user column value equal to 1.
$socials=Socials::model()->findAll('socials_user=:socials_user', array(':socials_user'=>1));
You are using a Model from YII. Models always represent one Object in YII. That's why it looks like that there is a LIMIT 1
as you described it.
You need to query the database instead to return a set of rows. Here is some YII database access example code to give a picture how that works (from here):
$dataReader=$command->query();
// calling read() repeatedly until it returns false
while(($row=$dataReader->read())!==false) { ... }
// using foreach to traverse through every row of data
foreach($dataReader as $row) { ... }
// retrieving all rows at once in a single array
$rows=$dataReader->readAll();
I do not know if you already have a database connection set up, but I assume so as you're already using a database. So this is what you need to access the DB Command object then to actually call query()
(from here):
$connection=Yii::app()->db; // assuming you have configured a "db" connection
// If not, you may explicitly create a connection:
// $connection=new CDbConnection($dsn,$username,$password);
$command=$connection->createCommand($sql);
// if needed, the SQL statement may be updated as follows:
// $command->text=$newSQL;
Hopefully this information if of use to you.
better yet, use findAllByAttributes.
your code should be:
$socials=Socials::model()->findAllByAttributes(array('socials_user'=>1));
Doc : http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findAllByAttributes-detail
精彩评论