【问题标题】:Error creating children relationship in Laravel 4 Eloquent ORM在 Laravel 4 Eloquent ORM 中创建子关系时出错
【发布时间】:2014-10-17 10:20:41
【问题描述】:

我有一个包含 2 个这样的模型的结构:

class Contenuti extends Eloquent {
    public function dettaglio()
    {
        return $this->hasOne('ContenutiDettaglio', 'contenuto_id');
    }
}

class ContenutiDettaglio extends Eloquent {
    public function contenuto()
    {
        return $this->belongsTo('Contenuti', 'contenuto_id', 'id');
    }
}    

我正在尝试扩展 Contenuti 的 findOrNew,以便在创建 Contenuti 时始终添加 ContenutiDettaglio 项。

我的扩展是这样写的:

public static function findOrNew($id, $coloumns = array())
{
    $obj = parent::findOrNew($id);

    if(!isset($obj->dettaglio()->id))
    {
      $obj->save();

      $dettaglio = ContenutiDettaglio::create(array('titolo' => 'Nuovo contenuto'));
      $dettaglio->contenuto()->associate($obj);

      $obj = Contenuti::findOrFail($obj->id);

      dd($obj->dettaglio());
    }

    return $obj;
}

在数据库中,ContenutiDettaglio 中的记录正确包含父 ID,但是当我转储 $obj->dettaglio() 时,它是空的。我做错了吗?

编辑:

这是我的迁移:

#Contenuti
public function up()
{
    Schema::create('contenuti', function($t) {
            $t->engine = 'MyISAM';

            // vecchia tabella "contenuti"    
            $t->increments('id');
            $t->bigInteger('userid')->nullable();
            $t->boolean('pubblica');

            // vecchia tabella "contenuti_it"
            $t->timestamp('pubblica_dal')->nullable();
            $t->timestamp('pubblica_al')->nullable();

            $t->timestamps();

            $t->index('pubblica_dal');
            $t->index('pubblica_al');
            $t->index('created_at');
            $t->index('updated_at');
    });
}

#ContenutiDettaglio
public function up()
{
    Schema::create('contenuti_dettaglio', function(Blueprint $table)
    {
        $table->engine = 'MyISAM';

        $table->increments('id');
        $table->integer('contenuto_id');
        $table->string('keywords', 255);
        $table->string('titolo', 255);
        $table->text('sottotitolo');
        $table->text('occhiello');
        $table->text('riassunto');
        $table->text('descrizione');

        $table->index('contenuto_id');
        $table->index('titolo');
    });
}

contenuti_dettaglio 中的 contenuto_id 是两个表之间的键。

【问题讨论】:

    标签: php laravel orm eloquent


    【解决方案1】:

    首先,这是 Eloquent 中关于关系的文档:http://laravel.com/docs/4.2/eloquent#relationships

    所以,hasOne 的基本语法是这样的:

    return $this->hasOne('ClassName', 'foreign_key', 'local_key');
    

    默认情况下,foreign_keylocal_key 等于“id”。 foreign_key 是关系的主键。

    对于belongsTo它是:

    return $this->belongsTo('ClassName', 'local_key', 'foreign_key');
    

    默认情况下,local_keyforeign_key 再次等于“id”。 Contenuti 模型中child 的key 由hasOne 方法的foreign_key 定义。我不知道您确切的数据库架构,但我想您可以通过更改来解决这个问题:

    return $this->belongsTo('Contenuti', 'contenuto_id', 'id');
    

    return $this->belongsTo('Contenuti', 'id', 'contenuto_id');
    

    让我知道这是否有效,如果无效,请为这些表附加您的迁移文件。

    【讨论】:

    • 感谢 hannesvdvreken 的回复,我已经尝试过您的解决方案,但没有成功。我已经发布了我的迁移...
    • 我已经通过这种方式解决了我的问题public static function findOrNew($id, $coloumns = array()) { $obj = parent::findOrNew($id); if($obj->id == NULL) { $obj->save(); $dettaglio = new ContenutiDettaglio(array('titolo' => 'Nuovo contenuto', 'contenuto_id' => $obj->id)); $dettaglio->save(); unset($obj); $obj = self::find($dettaglio->contenuto_id); } return $obj; }
    • 这不是真的推荐,但如果它对你有用并且你不打扰......那么我为你感到高兴:-)
    猜你喜欢
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    • 2015-04-04
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    相关资源
    最近更新 更多