Yii Framework Forum: New model with relations - Yii Framework Forum

Jump to content

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

New model with relations Rate Topic: -----

#1 User is offline   gums 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 04-October 10

Posted 28 October 2010 - 10:28 AM

Let us say I create a table User whose key is userid. There is another table called Project which has a foreign key userid which references User table.

Is it possible to create a new Model that includes both the tables or is a model always associated with a single table.

Thanks.
0

#2 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,448
  • Joined: 04-October 10

Posted 28 October 2010 - 12:24 PM

Yes, it is possible to create new Models and relate different tables as long as you specify all of them in your relations() function per model class.


So User will have a Project and also a Profile if Project and Profile are related to User and User relates to them in their relations() function respectively.
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#3 User is offline   gums 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 04-October 10

Posted 28 October 2010 - 02:55 PM

View PostAntonio Ramirez, on 28 October 2010 - 12:24 PM, said:

Yes, it is possible to create new Models and relate different tables as long as you specify all of them in your relations() function per model class.


So User will have a Project and also a Profile if Project and Profile are related to User and User relates to them in their relations() function respectively.


Let us say you want to create a new project.

Can you declare it like

$user = new User() in the controller and pass it to a form that lets you fill up the project details and save the record to a Project table. Here User is a model and project is declared as a relation to the user.
Thanks.
0

#4 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,448
  • Joined: 04-October 10

Posted 28 October 2010 - 03:27 PM

If a project already relates to a user I would just pass the id of the user the project needs to be related to, and then create a hidden field that will hold its user id with a name like Project[userId] (if that is the case) and then when user submits the Project form, it can just update its attributes as normally.

if you check the HTML code of the any form you will figure it out easily. You will see that field names are set in a way of ModelName[attribute]

It is just another way of doing it... without breaking your head against the wall.
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
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