如何构建cakephp 3中存在或等于数字的规则? [英] How to build rule exist in or equal to a number in cakephp 3?

查看:64
本文介绍了如何构建cakephp 3中存在或等于数字的规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有表条评论和列 parent_id

这是 CommentsTable.php 的内容:

namespace App\Model\Table;

use App\Model\Entity\Comment;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
 * Comments Model
 */
class CommentsTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        $this->table('comments');
        $this->displayField('id');
        $this->primaryKey('id');
        $this->belongsTo('Users', [
            'foreignKey' => 'user_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('Posts', [
            'foreignKey' => 'post_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('ParentComments', [
            'className' => 'Comments',
            'foreignKey' => 'parent_id'
        ]);
        $this->hasMany('ChildComments', [
            'className' => 'Comments',
            'foreignKey' => 'parent_id'
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator
            ->add('id', 'valid', ['rule' => 'numeric'])
            ->allowEmpty('id', 'create')
            ->requirePresence('body', 'create')
            ->notEmpty('body')
            ->requirePresence('path', 'create')
            ->notEmpty('path')
            ->add('status', 'valid', ['rule' => 'numeric'])
            ->requirePresence('status', 'create')
            ->notEmpty('status')
            ->add('created_at', 'valid', ['rule' => 'datetime'])
            ->requirePresence('created_at', 'create')
            ->notEmpty('created_at')
            ->add('updated_at', 'valid', ['rule' => 'datetime'])
            ->requirePresence('updated_at', 'create')
            ->notEmpty('updated_at');

        return $validator;
    }

    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->existsIn(['user_id'], 'Users'));
        $rules->add($rules->existsIn(['post_id'], 'Posts'));
        $rules->add($rules->existsIn(['parent_id'], 'ParentComments'));
        return $rules;
    }
}

我想为字段<$ c $建立规则c> parent_id :存在于ParentComments中或等于0。

I want to build rule for field parent_id: exist in ParentComments or equal to 0.

您能帮我吗?

非常感谢。

推荐答案

规则只是可调用的函数或可调用的类。 existsIn()函数只是 ExistsIn 类的别名。我们可以利用我们的优势:

Rules are just callable functions or callable classes. The existsIn() function is just an alias for the ExistsIn class. We can use the to our advantage:

...

use Cake\ORM\Rule\ExistsIn;
class CommentsTable extends Table
{
...
   public function buildRules(RulesChecker $rules)
    {
        ...
        $rules->add(
            function ($entity, $options) {
                $rule = new ExistsIn(['parent_id'], 'ParentComments');
                return $entity->parent_id === 1 || $rule($entity, $options);
            },
            ['errorField' => 'parent_id', 'message' => 'Wrong Parent']
        );
        return $rules;
    }
}

这篇关于如何构建cakephp 3中存在或等于数字的规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆