具有以下代码:
class SearchIndex extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'searchindex';
//no timestamps
public $timestamps = false;
//mass-assignment
public $fillable = array('url', 'content', 'version', 'digest');
public $guarded = array();
public static function updateIndex($url, $version, $content)
{
self::retrieveSearchEntry($url, $version)->updateContent($content)->save();
}
public static function retrieveSearchEntry($url, $version)
{
try
{
return self::withoutContent()->where('url', $url)->where('version', $version)->firstOrFail(array('url', 'version', 'digest'));
}
catch(\Illuminate\Database\Eloquent\ModelNotFoundException $e)
{
return new SearchIndex(array('url' => $url, 'version' => $version));
}
}
public function updateContent($content)
{
$hash = md5($content);
if (!isset($this->digest) || ($this->digest != $hash))
{
$this->content = $content;
$this->digest = $hash;
}
return $this;
}
public static function search($content)
{
}
}如果我调用updateIndex提供url+version的新组合,就会创建一个条目。
如果我调用updateIndex提供现有的一对url+version,则不会使用新的内容更新条目。我发现这与我忽略了“content”字段有关(原因:是巨大的,我想在这个函数中设置它,而不是获取它)。
问题:我怎么能-得到内容字段,但能够在保存时设置它?
发布于 2014-04-04 02:42:58
解决了。原因:在firstOrFail()中进行自定义选择时,我没有选择"id“字段。这样,id为null,生成的SQL试图更新(因为它是一个存在的对象)“其中id为空”,这将影响0行。
https://stackoverflow.com/questions/22852261
复制相似问题