Laravel验证:具有多个列和soft_delete的唯一

ppa*_*ida 2 validation laravel laravel-4

我正在尝试执行Laravel验证规则,如下所示:

"permalink" => "required|unique:posts,permalink,hotel_id,deleted_at,NULL|alpha_dash|max:255",
Run Code Online (Sandbox Code Playgroud)

对规则的解释是:

我的系统中有一个表"Posts",其中包含以下字段(其中包括):hotel_id,permalink,deleted_at.如果MySQL允许使用空值创建唯一索引,则sql将是:

ALTER TABLE `posts` 
ADD UNIQUE `unique_index`(`hotel_id`, `permalink`, `deleted_at`);
Run Code Online (Sandbox Code Playgroud)

所以:我刚加入新行IF:该组合hotel_id,permalinkdeleted_at场(巫婆必须为NULL)是唯一的.

如果已存在永久链接和hotel_id字段相同且'deleted_at'字段为NULL的行,则验证将返回FALSE,并且该行不会插入数据库中.

好.我不知道为什么,但是Laravel构建的查询看起来像:

SELECT count(*) AS AGGREGATE FROM `posts` 
WHERE `hotel_id` = the-permalink-value AND `NULL` <> deleted_at)
Run Code Online (Sandbox Code Playgroud)

有没有搞错...

我希望Laravel构建验证的查询是:

SELECT count(*) AS AGGREGATE FROM `posts` 
WHERE `permalink` = 'the-permalink-value' AND `hotel_id` = ? AND `deleted_at` IS NULL
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下这有效吗?因为到处都是我看起来像这样:

$rules = array(
   'field_to_validate' =>
   'unique:table_name,field,anotherField,aFieldDifferentThanNull,NULL',
);
Run Code Online (Sandbox Code Playgroud)

有人能帮助我吗?

谢谢

ppa*_*ida 8

所有.

最后,我对验证有了正确的理解(至少,我认为是这样),而且我有一个解决方案,如果它不漂亮,它可以帮助某人.

正如我之前所说,我的问题是验证某个列(永久链接)是否唯一,只要其他列值具有某些特定值.问题是Laravel验证字符串规则的工作方式.让我们来看看:

首先我写了这个:"permalink"=>"required | unique:posts,permalink,hotel_id,deleted_at,NULL | alpha_dash | max:255",

它产生了错误的查询.现在看看这个:

'column_to_validate' => 'unique:table_name,column_to_validate,id_to_ignore,other_column,value,other_column_2,value_2,other_column_N,value_N',
Run Code Online (Sandbox Code Playgroud)

所以.唯一字符串最初有3个参数:1)验证的表名称2)用于验证唯一值的列的名称3)要避免的列的ID(如果您正在编辑行,则不是创造一个新的).

在此之后,您所要做的就是将其他列按顺序放在"key,value"中,以便在您的唯一规则中使用.

哦,容易,一个?没那么快,爪子.如果您正在使用STATIC数组,那么您将如何获得"当前"ID以避免?因为Laravel Model中的$ rules数组是一个静态数组.所以,我不得不想出这个:

  public static function getPermalinkValidationStr() {
    $all = Input::all();
    # If you are just building the frozenNode page, just a simple validation string to the permalink field:
    if(!array_key_exists('hotel', $all)) {
      return 'required|alpha_dash|max:255';
    }

    /* Now the game got real: are you saving a new record or editing a field? 
If it is new, use 'NULL', otherwise, use the current id to edit a row.
*/
    $hasId = isset($all['id']) ? $all['id'] : 'NULL';

# Also, check if the new record with the same permalink belongs to the same hotel and the 'deleted_at' field is NULL:

    $result = 'required|alpha_dash|max:255|unique:posts,permalink,' . $hasId . ',id,hotel_id,' . $all['hotel'] . ',deleted_at,NULL';
    return $result;
  }
Run Code Online (Sandbox Code Playgroud)

并且,在FrozenNode规则配置中:

'rules' => array(
    'hotel_id' => 'required',
    'permalink' => Post::getPermalinkValidationStr()
  ),
Run Code Online (Sandbox Code Playgroud)

好.我不知道是否有最简单的方法(或更好的方法).如果您对此解决方案有任何不妥之处,请发表评论,我将很高兴听到更好的解决方案.我已经尝试过Ardent和Observer,但我遇到了FrozenNode Administrator的一些问题.

谢谢.