Yii Framework Forum: Problem with relations many to many - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem with relations many to many Rate Topic: -----

#1 User is offline   aniolekx 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 21-April 09

Posted 27 January 2010 - 02:55 AM

Could soomeone explain me how to build reations many to many. I cant find the proper way.

Is there problem with this database? because CRUD generated me relation only for category_product
all relations i post here i have made myself, but it dosent work anyway.

Blog is lacking of this kind relation, so i cant find good example... i found example in documentation but it is lacking for full example..


CREATE TABLE category (
 id BIGINT AUTO_INCREMENT,
 parent_id INT UNSIGNED DEFAULT '0' NOT NULL,
 category_name VARCHAR(32) NOT NULL,
 PRIMARY KEY(id),
 UNIQUE(category_name)
 ) DEFAULT CHARACTER SET utf8 ENGINE = INNODB;
 
 CREATE TABLE product (
 id BIGINT AUTO_INCREMENT,
 product_name VARCHAR(150) NOT NULL,
 PRIMARY KEY(id)
 ) DEFAULT CHARACTER SET utf8 ENGINE = INNODB;

CREATE TABLE category_product (
 category_id BIGINT COMMENT 'CONSTRAINT FOREIGN KEY (category_id) REFERENCES category(id)',
 product_id BIGINT COMMENT 'CONSTRAINT FOREIGN KEY (product_id) REFERENCES product(id)',
 INDEX (category_id),
 INDEX (product_id),
 PRIMARY KEY(category_id, product_id)
 ) DEFAULT CHARACTER SET utf8 ENGINE = INNODB;


ProductController.php
public function actionView()
	{
		$this->render('view',array(
			'model'=>$this->loadModel(),
		));
	}


public function loadModel()
	{
		if($this->_model===null)
		{
			if(isset($_GET['id']))
				$this->_model=product::model()->with('category')->together()->findbyPk($_GET['id']);
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model;
	}


Now snippet from model:

For category:
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(
			 'product'=>array(self::MANY_MANY, 'product',
                'category_product(category_id, product_id)'),
		);
	}


for product:
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(
                    'category'=>array(self::MANY_MANY, 'category',
                'category_product(product_id, category_id)'),

		);
	}


for category_product relation is empty

now in action show in template how i can access those values? Is this right?
$model->category->category_name

0

#2 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 27 January 2010 - 04:58 AM

<?php foreach($model->category as $category): ?>
<div>
<?php echo $category->name; ?>
</div>
<?php endforeach; ?>


something like that
php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
0

#3 User is offline   aniolekx 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 21-April 09

Posted 27 January 2010 - 08:28 AM

doesnt work, i think there is problem with relations
0

#4 User is offline   Jetfu 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 26-January 10

Posted 01 February 2010 - 12:54 PM

View Postaniolekx, on 27 January 2010 - 08:28 AM, said:

doesnt work, i think there is problem with relations


Does anyone have any examples for working with MANY_MANY relations? I have the same issue where I'm not getting any data back.
2

#5 User is offline   aniolekx 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 21-April 09

Posted 09 February 2010 - 03:41 AM

Could some1 explain how to build relations many to many in yii. Like i said before, tutorials are lacking for full example..

This problem stopped me to build shopping cart with yii, right now im working with symfony, because doctrine seems to be easier than ar in yii
-1

#6 User is offline   ariefpriyadi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 08-June 10
  • Location:Kenjeran-Surabaya

Posted 05 September 2010 - 09:28 PM

I have same Problem ..
in model
...
return array(
	'userJob' => array(self::HAS_ONE, 'jobtitle', 'EmployeeID' ),
);
...


but if i change to HAS_MANY there was no Result
EmployeeID    JobName
1
2
3
4


Anyone can help me??
arief.kenjeran.net
0

#7 User is offline   nomar 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 26-April 09

Posted 06 September 2010 - 11:03 AM

did you try adding a 'with' clause to your query?
0

#8 User is offline   ariefpriyadi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 08-June 10
  • Location:Kenjeran-Surabaya

Posted 06 September 2010 - 08:03 PM

View Postnomar, on 06 September 2010 - 11:03 AM, said:

did you try adding a 'with' clause to your query?


yes. i already did it. but no result
..
return array(
        'userJob' => array(self::HAS_ONE, 'jobtitle', 'EmployeeID' ,'with'=>'jobtitle'),
);
..


No result.. any one?
arief.kenjeran.net
0

#9 User is offline   nomar 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 26-April 09

Posted 07 September 2010 - 05:31 AM

The 'with' part makes no sense to me. In my understanding you are asking to also fetch an object jobtitle when you are fetching it anyway.
Also it would be easier to understand your problem when you would post more code, like models, controller parts and maybe the table structure.

Assuming ActiveRecords named 'Employee' and 'Job' something like this should work

$employee = Employee::model()->findByPk(1);
$employee->userJob->jobtitle;

0

#10 User is offline   nomar 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 26-April 09

Posted 07 September 2010 - 05:31 AM

removed duplicated posting.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users