Search code examples
laraveleloquentrelationships

elequent belongsTo returns key instead of object on live server, but model on development machine


I have some Models in Laravel, but one of them is behaving strange...


this is kinda embarrassing,

I have somehow managed to have different colum names on my production server from my Development machine... on my Development machine, the column is called creation_id, but on my production machine, it is called creation...


I have a Creation that has many Events the event belongs to the Creation and hasMany eventAttendees the eventAttendees belongs to events, and can access the creation->title that is is grand parent for convenience

on my Development machine, everything is working as I would expect, but on my live server, I get the id instead of the creation from Event->creation...

notice, that EventAttende->getEventCreationTitleAttribute works until 'getCreationTitleAttribute', this Means that EventAttende->event() works...

I am using laravel 4.1

This is the relevant code, the getCreationTitleAttribute is called from frozenNodes administrator

// Filename: /app/models/Event.php
<?php
namespace XXX;
class Event extends \Eloquent 
{
    protected $connection = 'legacy';

    public function creation()
    {
        return $this->belongsTo('XXX\Creation', 'creation_id');
    }

    public function getCreationTitleAttribute()
    {
        $x = $this->creation; // I have also tried $this->creation()->first() which returns null on live server

        // This should be the normal behaviour, the test shouldn't even be necessary
        // And the test wasn't there before i uploaded to my live server
        if (is_object($x))
            return $x->title;

        // This is the flow I would expect, 
        // and the flow I get on my production machine...
        return $x;
    }

    public function eventAttende()
    {
        return $this->hasMany('XXX\EventAttende');
    }
}

// Filename: /app/models/Creation.php
<?php
namespace XXX;
class Creation extends \Eloquent 
{
    protected $connection = 'legacy';

    public function Creation()
    {
        $this->bgColor="FFFFFF";
        $this->textColor="000000";
        $this->linkColor="0000FF";
    }

    public function events()
    {
        return $this->hasMany('XXX\Event');
    }
}

// Filename: /app/models/EventAttende.php
<?php
namespace XXX;
class EventAttende extends \Eloquent 
{
    protected $connection = 'legacy';

    // this works
    public function event()
    {
        return $this->belongsTo('XXX\Event', 'event');
    }

    public function getEventCreationTitleAttribute()
    {
        return $this->event()->first()->creationTitle;
    }
}

I am using frozennodes administrator to show the data. (The business code is an older legacy platform, so I don't use the data from laravel yet.)

the column definition part that uses the attribute is this:

/**
 * The display columns
 */
'columns' => array(
    'id',
    'creation_title' => array(
        'title' => 'Kreation',
        //'name_field' => 'full_name',
        //'type' => 'relationship',
        //'relationship' => 'creation',
    ),
    'name' => array(
        'title' => 'Navn'
    ),

),

Solution

  • I had somehow managed to have different colum names on my production server from my Development machine...

    • on my Development machine, the column is called creation_id,
    • on my production machine, it is called creation...