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

Auto setter for relations #125

Open
greut opened this issue Feb 18, 2011 · 12 comments
Open

Auto setter for relations #125

greut opened this issue Feb 18, 2011 · 12 comments

Comments

@greut
Copy link
Collaborator

greut commented Feb 18, 2011

I've got tired of writing custom setters and came up with this, like __get it uses the relationship to auto assign a value.

What do you think?

/**
 * Auto magically link them
 */
public function __set($name, $value)
{
    $table = static::table();

    if (is_object($value) && 
        $relationship = $table->get_relationship($name)
    ) {
        $class_name = get_class($value);
        if ($relationship->class_name === $class_name) {
            $this->__relationships[$name] = $value;
            if (!$value->is_new_record()) {
                $pk = $value->get_primary_key(0);
                $name = $relationship->foreign_key[0];
                $value = $value->{$pk[0]};
            } else {
                return;
            }
        }
    }

    parent::__set($name, $value);   
}
@greut
Copy link
Collaborator Author

greut commented Apr 6, 2011

@greut
Copy link
Collaborator Author

greut commented Apr 10, 2011

@jbtbnl
Copy link

jbtbnl commented Apr 13, 2011

Similar issue: #34

@tuupola
Copy link
Contributor

tuupola commented Nov 21, 2012

This still is not in official repo? Is PHP ActiveRecord still maintained?

@anther
Copy link
Contributor

anther commented Apr 4, 2013

+1!

@Rican7
Copy link
Collaborator

Rican7 commented Apr 17, 2013

+1
This really needs to be added.

@Rican7
Copy link
Collaborator

Rican7 commented Apr 17, 2013

@greut did you ever make a formal pull request based on this issue. I see that you've put it into a separate branch on your own repo and linked it here, but making a formal PR makes the issue much easier to fix/merge.

@al-the-x
Copy link
Collaborator

Yeah, @greut, if you could attach your branch to this issue to make it a pull request, that would really help.

@al-the-x
Copy link
Collaborator

If @gruet doesn't this week, I'll fetch, rebase, and open a PR for his work. This seems like a no-brainer to add, assuming there are tests, of course. ;)

@Rican7
Copy link
Collaborator

Rican7 commented Apr 27, 2013

Just a quick note, instead of simply checking if the $value is an object, I feel like it would make more sense to check if it was an ActiveRecord Model instance:

// OLD way of doing it
if (is_object($value) && 
        $relationship = $table->get_relationship($name)
    )

...

// The new hotness
if (($value instanceof \ActiveRecord\Model) && 
        $relationship = $table->get_relationship($name)
    )

Of course you don't have to fully qualify the Model namespace if its in the Model class already. Hell, you could just use self in that case. :P

@greut
Copy link
Collaborator Author

greut commented May 2, 2013

@al-the-x be my guest, I won't have time to do that anytime soon. Thanks!

@al-the-x
Copy link
Collaborator

Like your suggestion, @Rican7. Let's see which of us can get to it first...! 🐎

This was referenced Jul 17, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants