在CakePHP模型数组中添加PHP函数
我有一个行为数组$actas。问题是当我在数组字符串上添加date()函数时,它返回一个错误: 例如:在CakePHP模型数组中添加PHP函数,php,arrays,cakephp,model,Php,Arrays,Cakephp,Model,我有一个行为数组$actas。问题是当我在数组字符串上添加date()函数时,它返回一个错误: 例如: public $actas = array( 'Uploader.Attachment'=>array( 'books' => array( 'maxWidth' => 1200, 'maxHeight' => 1200, 'extension' => array('pdf'),
public $actas = array(
'Uploader.Attachment'=>array(
'books' => array(
'maxWidth' => 1200,
'maxHeight' => 1200,
'extension' => array('pdf'),
'nameCallback' => '',
'append' => '',
'prepend' => '',
'tempDir' => TMP,
'uploadDir' => '/var/www/html/apps/webroot/files/uploads/books' . date('d-m-Y'),//this is where I want to add the function.
'transportDir' => ''
)
)
);
但是它不起作用。我也喜欢这样:
public $actas = array(
'Uploader.Attachment'=>array(
'books' => array(
'maxWidth' => 1200,
'maxHeight' => 1200,
'extension' => array('pdf'),
'nameCallback' => '',
'append' => '',
'prepend' => '',
'tempDir' => TMP,
'uploadDir' => "/var/www/html/apps/webroot/files/uploads/books'".date('d-m-Y')."'",//this is where I want to add the function.
'transportDir' => ''
)
)
);
也不管用
所以我的问题是如何做到这一点?如果我有很多错误,请告诉我,这样我可以了解更多关于这件事
提前谢谢
这是Post.php模型的完整源代码
<?php
App::uses('AppModel', 'Model');
/**
* Post Model
*
* @property Tier $Tier
* @property Category $Category
* @property Comment $Comment
*/
class Post extends AppModel {
//var $now = 'CURDATE()';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'title' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'content' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Tier' => array(
'className' => 'Tier',
'foreignKey' => 'tier_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Category' => array(
'className' => 'Category',
'foreignKey' => 'category_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'Comment' => array(
'className' => 'Comment',
'foreignKey' => 'post_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
public $actsAs = array(
//'Containable',
'Uploader.Attachment' => array(
// Do not copy all these settings, it's merely an example
'banner' => array(
'maxWidth' => 1200,
'maxHeight' => 1200,
'extension' => array('gif', 'jpg', 'png', 'jpeg'),
'nameCallback' => '',
'append' => '',
'prepend' => '',
'tempDir' => TMP,
'uploadDir' => "/var/www/html/apps/webroot/img/banners/",
'transportDir' => '',
'finalPath' => '/img/banners/',
'dbColumn' => '',
'metaColumns' => array(),
'defaultPath' => '',
'overwrite' => true,
'transforms' => array(),
'stopSave' => true,
'allowEmpty' => true,
'transformers' => array(),
'transport' => array(),
'transporters' => array(),
'curl' => array()
),
'feature' => array(
'maxWidth' => 1200,
'maxHeight' => 1200,
'extension' => array('gif', 'jpg', 'png', 'jpeg'),
'nameCallback' => '',
'append' => '',
'prepend' => '',
'tempDir' => TMP,
'uploadDir' => '/var/www/html/apps/webroot/img/features/',
'transportDir' => '',
'finalPath' => '/img/features/',
'dbColumn' => '',
'metaColumns' => array(),
'defaultPath' => '',
'overwrite' => true,
'transforms' => array(),
'stopSave' => true,
'allowEmpty' => true,
'transformers' => array(),
'transport' => array(),
'transporters' => array(),
'curl' => array()
),
'books' => array(
'maxWidth' => 1200,
'maxHeight' => 1200,
'extension' => array('pdf'),
'nameCallback' => '',
'append' => '',
'prepend' => '',
'tempDir' => TMP,
'uploadDir' => '/var/www/html/apps/webroot/files/uploads/books' . date('d-m-Y'),
'transportDir' => '',
'finalPath' => '/files/uploads/books/',
'dbColumn' => '',
'metaColumns' => array(),
'defaultPath' => '',
'overwrite' => true,
'transforms' => array(),
'stopSave' => true,
'allowEmpty' => true,
'transformers' => array(),
'transport' => array(),
'transporters' => array(),
'curl' => array()
)
)
);
}
啊,好吧,我不是戴着OO帽子看的。您不能这样做,因为:
类成员变量称为“属性”。你也可以看到它们
指使用其他术语,如“属性”或“字段”,但
在本参考中,我们将使用“属性”。他们是
通过使用关键字public、protected或private之一定义,
后跟一个普通变量声明。本声明可以
包括初始化,但此初始化必须是
常量值——也就是说,它必须能够在编译时进行计算
时间,并且必须不依赖于运行时信息才能
已评估。
您需要使用\u construct()
方法对我有效:(忽略通知)您确定要向我们显示生成错误的代码吗?它仍然不起作用。请继续打印致命错误错误:语法错误,意外“.”,期望“)”发布完整代码,您发布的代码不是错误的原因谢谢您,先生。但我不知道如何实现。如果您不介意,您可以分享一些参考资料如何做到这一点,以便我可以阅读和进行研发。再次感谢。
Properties