我在Yii2中有一个模型,我打印出来了。我想获得存储在logo属性中的值test.jpg。我怎样才能从这个模型中获得这个值。我尝试过pirnt_r($ model->属性),但它在logo字段中返回空值。有没有办法跳过log的inatail空值并获得logo字段的test.jpg值。
backend\models\UserProfiles Object
(
[logo] =>
[cover] =>
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 2
[date_created] => 2017-04-19 08:02:29
[status] => 1
[logo] => test.jpg
[cover] => test (2).jpg
[website] => wqeqww
[address] => qweqweqw
[about] => qweqwe
[phone] => qweqweq
[full_name] => qweqwe
[city] => qweqweq
[country] => qweqeqe
[profile_type] => Type 1
[user_id] => 2
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 2
[date_created] => 2017-04-19 08:02:29
[status] => 1
[logo] => test.jpg
[cover] => test (2).jpg
[website] => wqeqww
[address] => qweqweqw
[about] => qweqwe
[phone] => qweqweq
[full_name] => qweqwe
[city] => qweqweq
[country] => qweqeqe
[profile_type] => Type 1
[user_id] => 2
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] => ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
[0] => yii\validators\RequiredValidator Object
(
[skipOnEmpty] =>
[requiredValue] =>
[strict] =>
[message] => {attribute} cannot be blank.
[attributes] => Array
(
[0] => id
[1] => user_id
)
[on] => Array
(
)
[except] => Array
(
)
[skipOnError] => 1
[enableClientValidation] => 1
[isEmpty] =>
[when] =>
[whenClient] =>
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] =>
)
[1] => yii\validators\NumberValidator Object
(
[integerOnly] => 1
[max] =>
[min] =>
[tooBig] =>
[tooSmall] =>
[integerPattern] => /^\s*[+-]?\d+\s*$/
[numberPattern] => /^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/
[attributes] => Array
(
[0] => id
[1] => user_id
)
[message] => {attribute} must be an integer.
[on] => Array
(
)
[except] => Array
(
)
[skipOnError] => 1
[skipOnEmpty] => 1
[enableClientValidation] => 1
[isEmpty] =>
[when] =>
[whenClient] =>
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] =>
)
[2] => yii\validators\StringValidator Object
(
[length] =>
[max] =>
[min] =>
[message] => {attribute} must be a string.
[tooShort] =>
[tooLong] =>
[notEqual] =>
[encoding] => UTF-8
[attributes] => Array
(
[0] => address
[1] => about
)
[on] => Array
(
)
[except] => Array
(
)
[skipOnError] => 1
[skipOnEmpty] => 1
[enableClientValidation] => 1
[isEmpty] =>
[when] =>
[whenClient] =>
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] =>
)
[3] => yii\validators\FileValidator Object
(
[extensions] => Array
(
[0] => jpg
[1] => png
)
[checkExtensionByMimeType] => 1
[mimeTypes] => Array
(
)
[minSize] =>
[maxSize] =>
[maxFiles] => 1
[message] => File upload failed.
[uploadRequired] => Please upload a file.
[tooBig] => The file "{file}" is too big. Its size cannot exceed {formattedLimit}.
[tooSmall] => The file "{file}" is too small. Its size cannot be smaller than {formattedLimit}.
[tooMany] => You can upload at most {limit, number} {limit, plural, one{file} other{files}}.
[wrongExtension] => Only files with these extensions are allowed: {extensions}.
[wrongMimeType] => Only files with these MIME types are allowed: {mimeTypes}.
[attributes] => Array
(
[0] => logo
[1] => cover
)
[on] => Array
(
)
[except] => Array
(
)
[skipOnError] => 1
[skipOnEmpty] =>
[enableClientValidation] => 1
[isEmpty] =>
[when] =>
[whenClient] =>
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] =>
)
[4] => yii\validators\StringValidator Object
(
[length] =>
[max] => 45
[min] =>
[message] => {attribute} must be a string.
[tooShort] =>
[tooLong] => {attribute} should contain at most {max, number} {max, plural, one{character} other{characters}}.
[notEqual] =>
[encoding] => UTF-8
[attributes] => Array
(
[0] => date_created
[1] => status
[2] => logo
[3] => cover
[4] => website
[5] => phone
[6] => full_name
[7] => city
[8] => country
[9] => profile_type
)
[on] => Array
(
)
[except] => Array
(
)
[skipOnError] => 1
[skipOnEmpty] => 1
[enableClientValidation] => 1
[isEmpty] =>
[when] =>
[whenClient] =>
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] =>
)
[5] => yii\validators\ExistValidator Object
(
[targetClass] => backend\models\User
[targetAttribute] => Array
(
[user_id] => id
)
[filter] =>
[allowArray] =>
[targetAttributeJunction] => and
[attributes] => Array
(
[0] => user_id
)
[message] => {attribute} is invalid.
[on] => Array
(
)
[except] => Array
(
)
[skipOnError] => 1
[skipOnEmpty] => 1
[enableClientValidation] => 1
[isEmpty] =>
[when] =>
[whenClient] =>
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] =>
)
)
)
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
答案 0 :(得分:1)
您看到的内容是私有属性_attributes BaseActiveRecord的一部分,无法直接访问。 看看yii \ db \ BaseActiveRecord类,你可以看到如何访问logo属性。
你可以尝试:
$模型 - >的getAttribute( '标志')
或者也许吧 $模型 - >标志
后者应该使用ActiveBaseRecord的魔术方法“__get”,它可能会起作用。我看到的问题是你的类还有一个名为logo的属性,这可能会使这变得复杂。