laracasts/TestDummy

Relationships using Factories

Closed this issue · 2 comments

In the Builder.php class, in the persist function, there's the following:

    protected function persist($name, array $attributes = [])
    {
        $entity = $this->build($name, $attributes);
        $attributes = $this->database->getAttributes($entity);

        // We'll filter through all of the columns, and check
        // to see if there are any defined relationships. If there
        // are, then we'll need to create those records as well.
        foreach ($attributes as $columnName => $value)
        {
            if ($relationship =  $this->hasRelationshipAttribute($value))
            {
                $entity[$columnName] = $this->fetchRelationship($relationship);
            }
        }
        $this->database->save($entity);
        return $entity;
    }

Instead of $this->database->getAttributes($entity), shouldn't it be getting the key/values from the factory and not the attributes from the database? Then when it loops through those key/values, it's able to find the 'factory: ' relationships that are defined in our factories file?

Sorry for delay.

Is there something specific that's not working for you - to the point that this creates a problem?

(Closing until you respond.)

Maybe I missed something, but because of this, the Builder wasn't able to find any relationships since it was looking in the wrong place for them.