Posts Tagged with relation

Displaying 1-7 of 7 results.
Wiki: CActiveRelation HAS_MANY BELONGS_TO based on secondary keywiki
posted by admin on November 16, 2016
In the Model class, I overrode the getTableSchema and manually defined the foreign keys like so:

public function getTableSchema()
{
$table = parent::getTableSchema();

$table->columns['sid']->isForeignKey = true;
$table->foreignKeys['sid'] = array('Click', 'm_sid');

return $table;
}

And in the corresponding table:

public function getTableSchema()
{
$table = parent::getTableSchema();

$table->columns['m_sid']->isForeignKey = true;
$table->foreignKeys['m_sid'] = array('Message', 'sid');

return $table;
}

Now the standard relation definitions work directly!

'clicks'=>array( self::HAS_MANY, 'Click', 'm_sid' ),

as well as

'message'=>array( self::BELONGS_TO, 'Message', 'sid'),
Read more
Wiki: Yii 1.1 right way to pass parametr to model search from relation controller and view (usage in CGridView)wiki
posted by admin on November 9, 2016
Models:

* @property integer $id
* @property integer $type_id
* @property string $name
class Store extends CActiveRecord
{
//...
public function rules()
{
return array(
//your rules
array('id, type_id, name', 'safe', 'on'=>'search, searchbytype'),
);
}

public function relations()
{
return array(
'type' => array(self::BELONGS_TO, 'Type', 'type_id'),
);
}

public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('t.id',$this->id);
$criteria->compare('t.name',$this->name,true);
$criteria->compare('t.type_id',$this->type_id);

return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'sort' => array(
'defaultOrder' => 't.id DESC',
'attributes' => array(
'*',
),
),
'pagination' => array (
'PageSize' => 50 //edit your number items per page here
),
));
}

//Our custom search with recieve passed parametrs
public function searchbytype($id)
{
$criteria=new CDbCriteria;
//add condition here
$criteria->condition = "t.type_id = ".$id;

$criteria->compare('t.id',$this->id);
$criteria->compare('t.name',$this->name,true);
$criteria->compare('t.type_id',$this->type_id);

//do not add condition here, filter search not working correct
//$criteria->condition = "t.type_id = ".$id;

return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'sort' => array(
'defaultOrder' => 't.id DESC',
'attributes' => array(
'*',
),
),
'pagination' => array (
'PageSize' => 50 //edit your number items per page here
),
));
}

//...
}

class Type extends CActiveRecord
{
//...
public function relations()
{
return array(
'store' => array(self::HAS_MANY, 'Store', 'type_id'),
);
}
//...
}


Controller:

class TypeController extends Controller
{
//...
public function actionView($id)
{
$model = $this->loadModel($id);
$stores=new Store('searchbytype');
$stores->unsetAttributes(); // clear any default values
if(isset($_GET['Store']))
$stores->attributes=$_GET['Store'];
$this->render('view',array(
'model'=>$model,
'stores'=>$stores
));
}
//...
}

View:

<h1><?php echo $model->id; ?></h1>
<?php
$id = Yii::app()->request->getQuery('id');

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'application-grid',
'dataProvider'=>$stores->searchbytype($id),
'filter'=>$stores,
'pagerCssClass' => 'pagination pull-right',

'columns'=>array(
'id',
'name',
array(
'class'=>'CButtonColumn',
),
),
));
Read more
Resolved: Yii 1.1 Relation to same model relations HAS_MANY BELONGS_TO error
posted by admin on October 12, 2016
I have this error
Trying to get property of non-object
php code

($model->creator_id) ? $model->creator->firstname . ' ' . $model->creator->lastname : 'Нет',

model relations:

public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'creator' => array(self::BELONGS_TO, 'User', 'creator_id'),
'creator' => array(self::HAS_MANY, 'User', 'creator_id'),
);
}
Read more
Resolved: MySQL get most popular tags ids combination with their counts
posted by admin on April 18, 2016
I have three tables
1.tbl_tag

id int NOT NULL AI PK
name VARCHAR(50) NOT NULL

id name
1 lifestyle
2 store
3 car
4 luxury
5 classy
6 boy
7 girl
8 man
9 woman
10 strong
etc ...


2.tbl_media

id int NOT NULL AI PK
name VARCHAR(50) NOT NULL

id name
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine
10 ten
etc...


3. tbl_media_tag

id int NOT NULL AI PK
media_id int NOT NULL -> relationship with tbl_media
tag_id int NOT NULL -> relationship with tbl_tag

id media_id tag_id
1 1 4
2 1 3
3 4 3
4 5 2
5 6 8
6 4 4
7 8 7
8 7 4
9 7 3
10 6 2
11 8 4
12 8 3
etc...


I want to get most popular tags(tag_id) combinations with LIMIT, for example most popular top ten tags combination

I want to get result like this

4, 7, 3 (75)
4, 8 (42)
1, 7, 9, 10 (28)
7, 8, 9 (10)
7, 2 (2)
Read more
Resolved: Yii 1.1 CHtml listData with relation table
posted by admin on April 5, 2016
I have the list of array from my model

CHtml::listData($tags, 'id', 'post_id')


In model

/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'post' => array(self::BELONGS_TO, 'Post', 'post_id'),
);
}


I want to get listData like this

CHtml::listData($tags, 'id', /*here is syntax to get relation name*/)
Read more
Resolved: Post views counts in Mysql. Better way to do it?
posted by admin on March 23, 2016
I have blog. And I want to construct my views count table. Please help me make it better.

id, name, ..., views, ...

I don't want structure like this, I want individual tableRead more
Resolved: Three table MySQL relation task, city, product, sale max join query
posted by admin on March 11, 2016
We have three tables, city, product, sale

city
  • id PK AI INT(10)
  • name VARCHAR(50)


product
  • id PK AI INT(10)
  • name VARCHAR(50)


sale
  • id PK AI INT(10)
  • city_id INT(10)
  • product_id INT(10)
  • amount INT(10)
  • total_price INT(10)


Task, Задача

Написать SQL-запрос, который выведет результат вида: город, товар, количество проданного товара, итоговая сумма. Т.е. для каждого города и для каждого товара нужно вывести сколько было продано данного товара и на какую сумму. Если в каком-то городе какой-то товар не был продан вовсе — выводить нули в полях "количество" и "сумма".
Read more