如何检查模型中的某个字段是否为空?

时间:2022-08-04 15:31:11

I have a model, $userModel. I want to check if any of the fields for this model are empty or null.

我有一个模型,$userModel。我要检查这个模型的任何字段是否为空或空。

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()-> arealfieldsfilled ();


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.

额外信息:模型已经保存在db中,不需要用户输入。这只是我检查一个特定的模型有多完整,并不是所有这些字段都需要在数据库中,只有少数几个。像$userModel->bio这样的东西通常是空的。

I want to avoid checking 5 to 10 fields. I don't want a giant if that has to be maintained when the model changes.

我想避免检查5到10个字段。如果模型改变了,我不想要一个巨人。

3 个解决方案

#1


2  

PHP allows you to iterate over an object's properties. The check for each property could be simplified using empty():

PHP允许对对象的属性进行迭代。使用empty()可以简化对每个属性的检查:

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

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

#2


1  

Use empty()

使用空()

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

empty() 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)
  • 阵列()(一个空数组)
  • $var; (a variable declared, but without a value)
  • $ var;(声明的变量,但没有值)

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()

#3


0  

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(). Moreover you can specify what rules scenario you wish to use. For example: $model->senario = 'search'; will be used validation rule search and property name should be array. But when scenario insert name should be string with length not more than 100.

当您使用这个模型时,您只需要通过$model->validate()来验证这个模型,如果它失败了—用$model->getErrors()来显示错误。此外,您还可以指定希望使用的规则场景。例如:$model->senario = 'search';将使用验证规则搜索和属性名应该是数组。但是,当场景插入名称应该是长度不超过100的字符串时。

In my example fields: name, country - required for insert (['name, country', 'on' => 'insert']).

在我的示例字段中:name, country—插入所需的(['name, country', 'on' => 'insert'])。

#1


2  

PHP allows you to iterate over an object's properties. The check for each property could be simplified using empty():

PHP允许对对象的属性进行迭代。使用empty()可以简化对每个属性的检查:

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

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

#2


1  

Use empty()

使用空()

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

empty() 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)
  • 阵列()(一个空数组)
  • $var; (a variable declared, but without a value)
  • $ var;(声明的变量,但没有值)

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()

#3


0  

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(). Moreover you can specify what rules scenario you wish to use. For example: $model->senario = 'search'; will be used validation rule search and property name should be array. But when scenario insert name should be string with length not more than 100.

当您使用这个模型时,您只需要通过$model->validate()来验证这个模型,如果它失败了—用$model->getErrors()来显示错误。此外,您还可以指定希望使用的规则场景。例如:$model->senario = 'search';将使用验证规则搜索和属性名应该是数组。但是,当场景插入名称应该是长度不超过100的字符串时。

In my example fields: name, country - required for insert (['name, country', 'on' => 'insert']).

在我的示例字段中:name, country—插入所需的(['name, country', 'on' => 'insert'])。