在 CakePHP 模型数组中添加 PHP 函数


Add PHP function in CakePHP model array

我有一系列行为$actas。问题是当我在数组字符串上添加 date() 函数时,它返回一个错误:

例:

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帽子看它。 您不能这样做,因为:

Properties

类成员变量称为"属性"。您可能还会看到它们 指使用其他术语,例如"属性"或"字段",但 出于此参考的目的,我们将使用"属性"。他们是 通过使用关键字之一定义 公共、受保护 或 私有, 后跟一个正态变量声明。本声明可以 包括初始化,但此初始化必须是 常量值 - 也就是说,它必须能够在编译时进行评估 时间,并且不得依赖于运行时信息才能 评价。

您需要使用 __construct() 方法