Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

has_many_through and is_valid fixes #303

Closed

Conversation

iazel
Copy link

@iazel iazel commented Apr 25, 2013

has_many_through rely on other relationship to get the correct keys, but this means that every relationship of this kind must reside both in model A and model B.
An example from foo_helper:

class User extends \ActiveRecord\Model {
    static $has_many = array(
        array('user_newsletters'),
        array('newsletters', 'through' => 'user_newsletters'),
        array('active_services'),
        array('services', 'through' => 'active_services')
    );

}

class Service extends \ActiveRecord\Model {
    static $has_many = array(
        array('active_services', 'foreign_key' => 'serv_id'),
        array('users', 'through' => 'active_services')
    );
}
class ActiveService extends \ActiveRecord\Model {
    static $belongs_to = array(
        array('user'),
        array('service')
    );
}

As you can see, the foreign key in active_services is "serv_id". Then User model will use that key for joins.
Plus, is possible use this kind of relationship with "joins" option:

# code is an attribute of services
User::all( array('joins' => array('services'), 'conditions' => 'code = "daily_mail"') );

Model's is_valid and is_invalid have been fixed. Now they do not revalidate model at each call, but only when validation is required (ex: first time, or when an attribute has been modified). This methods accept a parameter that if true will run validations; useful only when a virtual attribute change and is used in some validations (because that can't be tracked).


Test included, for more information see them: HasManyThrough - Validations

@al-the-x
Copy link
Collaborator

Terrific work @lazel! I'm glad to see you didn't give up after all...! Look forward to reviewing these changes and merging into master.

@al-the-x
Copy link
Collaborator

Related to @iazel's individual PRs: #290 and #283

@iazel
Copy link
Author

iazel commented Apr 26, 2013

Thank you @al-the-x :) Has many through is also better than the previous request of merging, this is "smarter" :) However, digging in code, I think eager load of this kind of relationship doesn't work properly: I'll do more research when I've some other free time!

@al-the-x
Copy link
Collaborator

Looks like the build is failing because the scema files don't include one of your tables. Could you include that schema in your PR as well?

@iazel
Copy link
Author

iazel commented Apr 27, 2013

Yes, I've already updated it in my fork, but I'm still new in using github and when I've requested the merging, instead of master, I set a particular commit and I haven't found a way to change/update it ^^"

EDIT: I'm glad I was wrong: eager load work properly :)

@ghost ghost assigned al-the-x May 18, 2013
@al-the-x
Copy link
Collaborator

Assigning to me to coach @iazel through sorting his Github rebasing issues...

@al-the-x al-the-x mentioned this pull request Jul 29, 2013
@sweiguny
Copy link

sweiguny commented Feb 6, 2014

are there any news concerning this merge requests?

@koenpunt koenpunt mentioned this pull request Jul 17, 2014
27 tasks
@koenpunt
Copy link
Collaborator

koenpunt commented Dec 6, 2014

Working on this: php-activerecord/Iazel-hmt-and-is_valid-fixes

@koenpunt koenpunt mentioned this pull request Dec 6, 2014
@koenpunt
Copy link
Collaborator

koenpunt commented Dec 7, 2014

Closing in favor of #464

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants