Data Base Relation

hi , i have 5 tables here and i want access from post model to image(from profile model)

how can i this work ? how shuold use relation ??

i cant bring back three table from post for example i can access to user table but not to profile !!

This should help:

Profile,

'user' => [self::BELONGS_TO, 'User', 'user_id']

Content

'user' => [self::BELONGS_TO, 'User', 'user_id']

Post

'content' => [self::BELONGS_TO, 'User', 'user_id']

User

 'profile' => [self::HAS_ONE, 'Profile', ['user_id'=>'id]

this relation for User model is made assuming that User can have just one profile. If it’s not right, than U should use HAS_MANY.

This should allow U to get image like this:

$post = Post::model()->findByPk(X);

$image = $post->content->user->profile->image;

1 Like

tanx dragan but i have this error again :


Property "User.profile" is not defined. 



when i use : $post->content->user->profile->image ; :-[

what’s your idea about ‘through’ ??? how can i use through for this!?

What you want exactly, can you written in clear way??

[list=1]

[*]Where is your image url in tables.

[*]What is node_id.

[/list]

Will help you to resolve your query, but post your requirement in clear way.

tanx dragan but i have this error again :

Property "User.profile" is not defined.

when i use : $post->content->user->profile->image ; :-[

what’s your idea about ‘through’ ??? how can i use through for this!?

This can only mean that U didn’t create relation right. Check relation name in User model

1 Like

i want to view posts on page . every post have content,node(category) ,author(user) and i should gather information from other foe view a post with completely information like author name, author image address ,…

is it possible that problem from here :

in my controller :

$data=$model->model()->with(array(‘content’))->findAll();

i use this code for relation too :




            "content"=>array(self::BELONGS_TO,"Content","content_id"),

            "user"=>array(self::BELONGS_TO,"User",array('user_id'=>'id'),"through"=>'content'),

            "profile"=>array(self::HAS_ONE,"User",array('id'=>'user_id'),"through"=>'user'),



but other error :




CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'profile.user_id' in 'on clause'. The SQL statement executed was: SELECT `t`.`id` AS `t0_c0`, `t`.`content_id` AS `t0_c1`, `t`.`node_id` AS `t0_c2`, `content`.`id` AS `t1_c0`, `content`.`title` AS `t1_c1`, `content`.`content` AS `t1_c2`, `content`.`user_id` AS `t1_c3`, `content`.`date` AS `t1_c4`, `user`.`id` AS `t2_c0`, `user`.`active` AS `t2_c1`, `user`.`login` AS `t2_c2`, `user`.`password` AS `t2_c3`, `user`.`is_superadmin` AS `t2_c4`, `profile`.`id` AS `t3_c0`, `profile`.`active` AS `t3_c1`, `profile`.`login` AS `t3_c2`, `profile`.`password` AS `t3_c3`, `profile`.`is_superadmin` AS `t3_c4` FROM `post` `t` LEFT OUTER JOIN `content` `content` ON (`t`.`content_id`=`content`.`id`) LEFT OUTER JOIN `user` `user` ON (`user`.`id`=`content`.`user_id`) LEFT OUTER JOIN `user` `profile` ON (`user`.`id`=`profile`.`user_id`) 



however profile table havent any column like this :




`profile`.`login` AS `t3_c2`, `profile`.`password` AS `t3_c3`, `profile`.`is_superadmin` AS `t3_c4`



these are user’s columns ??? ??? :o >:(

ooooo sorry my problem here :

“profile”=>array(self::HAS_ONE,“User”,array(‘id’=>‘user_id’),“through”=>‘user’),

that true is :

“profile”=>array(self::HAS_ONE,“Profile”,array(‘id’=>‘user_id’),“through”=>‘user’),

but Dragan i don’t know why your way is not work.

tank you all and sorry !