简体   繁体   English

如何检查模型的任何字段是空还是空?

[英]How to check if any of the fields of a model are null or empty?

I have a model, $userModel . 我有一个模型, $userModel I want to check if any of the fields for this model are empty or null . 我想检查此模型的任何字段是空还是null

At the moment I am doing it with a big if statement. 目前我正在做一个很大的if声明。

if(!empty($userModel->name) && !empty($userModel->address) ... && !empty($userModel->email))
{
   // All fields have values
}

This way works, but if later I need to add another field to the model, then I need to go back to the if and add another && condition there. 这种方式有效,但如果以后我需要在模型中添加另一个字段,那么我需要回到if并在那里添加另一个&&条件。

How can I do this in one check? 我怎么能在一张支票上做到这一点?

Is there something like: $userModel::model()->areAllFieldsFilled(); 有类似的东西: $userModel::model()->areAllFieldsFilled();


Extra info: The model is already saved in the db, and there is no need for user input. 额外信息:模型已保存在数据库中,无需用户输入。 This is just me checking how complete a particular model is, by no means will all these fields be required in the database, only a few. 这只是我检查特定模型的完整程度,绝不会在数据库中只需要几个字段。 Things like $userModel->bio are usually left null . $userModel->bio这样的东西通常都是null

I want to avoid checking 5 to 10 fields. 我想避免检查5到10个字段。 I don't want a giant if that has to be maintained when the model changes. 如果在模型改变时必须维护,我不想要一个巨人。

PHP allows you to iterate over an object's properties . PHP允许您迭代对象的属性 The check for each property could be simplified using empty() : 使用empty()可以简化每个属性的检查:

$allHaveValues = TRUE;
foreach ($userModel as $key => $value) {
    if (empty($value)) {
       $allHaveValues = FALSE;
       break;
    }
}

if ($allHaveValues) {
    // Do something...
}

Use empty() 使用empty()

if(!empty($userModel->name)) { .. }

empty() DOCS 空()DOCS

  • "" (an empty string) “”(空字符串)
  • 0 (0 as an integer) 0(0为整数)
  • 0.0 (0 as a float) 0.0(0作为浮点数)
  • "0" (0 as a string) “0”(0作为字符串)
  • NULL 空值
  • FALSE
  • array() (an empty array) array()(一个空数组)
  • $var; 是$ var; (a variable declared, but without a value) (声明的变量,但没有值)

Update 更新

$modelData = array($userModel->name, $userModel->address, $userModel->email);
if(!in_array('', $modelData) && !in_array(null, $modelData)) { .. }

in_array() in_array()

Or you can use array_intersect - 或者你可以使用array_intersect -

if(empty(array_intersect(array('', null), $modelData))) { .. }

array_intersect() array_intersect()

I think you don't need do it. 我想你不需要这样做。
Everything you need - it just specify your validation rules. 您需要的一切 - 它只是指定您的验证规则。
For example: 例如:

<?php

class Brand extends CActiveRecord
{
    public function tableName()
    {
        return 'brand';
    }

    public function rules()
    {
        return [
            ['name, country', 'on' => 'insert'],
            ['name', 'type', 'type' => 'string', 'on' => 'insert'],
            ['name', 'length', 'max' => 100, 'on' => 'insert'],
            ['name', 'type', 'type' => 'array', 'on' => 'search'],
            ['country', 'type', 'type' => 'string'],
            ['country', 'length', 'max' => 50],
        ];
    }
}

When you will use this model you'll just need validate this model by $model->validate() and if it fails - show errors with $model->getErrors() . 当您使用此模型时,您只需要通过$model->validate()验证此模型,如果失败 - 使用$model->getErrors()显示错误。 Moreover you can specify what rules scenario you wish to use. 此外,您可以指定要使用的规则方案。 For example: $model->senario = 'search'; 例如: $model->senario = 'search'; will be used validation rule search and property name should be array. 将使用验证规则search和属性name应该是数组。 But when scenario insert name should be string with length not more than 100. 但是当场景insert名称应该是长度不超过100的字符串。

In my example fields: name, country - required for insert ( ['name, country', 'on' => 'insert'] ). 在我的示例字段中:名称,国家/地区 - 插入所需( ['name, country', 'on' => 'insert'] )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM