CakePHP 3 - 属性未显示在表

时间:2017-02-17 09:37:36

标签: cakephp datatables attributes cakephp-3.x

我有一个名为Bookings的表,其中包含以下属性:

  • id
  • artist_id - 外键
  • status
  • amount
  • created
  • modified

它与表格ArtistsPaymentsSessions相关联。

在View中,我使用了jQuery插件DataTables来显示满足以下条件的Bookings

  • 他们的status等于'已确认'
  • 关联表Session's属性date_end必须大于当前日期。

但是,在加载页面时,amount属性显示为$ 0.00,基本上为null,但其他所有内容都显示正常。在CakePHP变量中,属性amount也显示为空白。但在MySQL中,明确指出amount不是空白。

以下是我的观点摘录:

<table class="bookingsTables display" id="confirmedTable">
    <thead>
        <tr>
            <th scope="col"><?= $this->Paginator->sort('name', 'Artist') ?></th>
            <th scope="col"><?= $this->Paginator->sort('date_start', 'Start Date') ?></th>
            <th scope="col"><?= $this->Paginator->sort('date_end', 'End Date') ?></th>
            <th scope="col"><?= $this->Paginator->sort('studio_id', 'Studio') ?></th>
            <th scope="col"><?= $this->Paginator->sort('engineer_id', 'Engineer') ?></th>
            <th scope="col"><?= $this->Paginator->sort('amount', 'Total Amount') ?></th>
            <th scope="col"><?= $this->Paginator->sort('status', 'Payment Status') ?></th>
            <th scope="col" class="actions"><?= __('Actions') ?></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($bookingsConfirmed as $booking): ?>
        <tr>
            <td><?= $booking->has('artist') ? h($booking->artist->name) : '' ?></td>
            <td><?= $booking->has('session') ? h($booking->session->date_start) : '' ?></td>
            <td><?= $booking->has('session') ? h($booking->session->date_end) : '' ?></td>
            <td><?= $booking->session->has('studio') ? h($booking->session->studio->name) : '' ?></td>
            <td><?= $booking->session->has('engineer') ? h($booking->session->engineer->eng_firstname . ' ' . $booking->session->engineer->eng_lastname) :'' ?></td>
            <td><?= h($this->Number->currency($booking->amount)) ?></td>
            <td><?= h($paymentStatusLookup[$booking->id]); ?></td>
            <td class="actions">
                <?= $this->Html->link(__('Update'), ['action' => 'bookingconfirm', $booking->id]) ?>
                <?= $this->Html->link(__('View'), ['action' => 'view', $booking->id]) ?>
                <p>Cancel</p>
            </td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
  

$ paymentStatusLookup [$ booking-&GT; ID];

以上行是付款的虚拟状态,但我不确定这是否与我的金额值缺失有关。我从我的表中删除了这一行,看看它是否有任何区别(我有两个显示相似内容的其他DataTables,虽然条件略有不同,amount显示这些表格正常),但事实并非如此。

  

$这 - &GT;&号 - GT;货币

这只是将null / empty字段变为$ 0.00。

在我的控制器中,该特定表的相应查找:

$bookingsConfirmed = $this->Bookings->find('all',[
            'contain' => ['Sessions', 'Sessions.Studios', 'Sessions.Engineers', 'Artists'],
            'conditions'=>['status' => 'confirmed', date_end >=' => date('Y-m-d H:i:s', Time::now()->getTimestamp())],
            'order'=>['Bookings.created'=>'ASC']
        ]);

在我的预订表中,在validationDefault函数中:

public function validationDefault(Validator $validator)
    {
        $validator
            ->integer('id')
            ->allowEmpty('id', 'create');

        $validator
            ->requirePresence('status', 'create')
            ->notEmpty('status');

        $validator
            ->decimal('amount')
            ->allowEmpty('amount');

        return $validator;
    }

我最近还在预订模式中做了一个Cake烘焙。

更新:以下是$ bookingsConfirmed:

的调试结果
object(Cake\ORM\Query) {

    '(help)' =&gt; 'This is a Query object, to get the results execute or iterate it.',
    'sql' =&gt; 'SELECT Bookings.id AS `Bookings__id`, Bookings.artist_id AS `Bookings__artist_id`, Bookings.status AS `Bookings__status`, Bookings.amount AS `Bookings__amount`, Bookings.created AS `Bookings__created`, Bookings.modified AS `Bookings__modified`, Sessions.id AS `Sessions__id`, Sessions.booking_id AS `Sessions__booking_id`, Sessions.studio_id AS `Sessions__studio_id`, Sessions.engineer_id AS `Sessions__engineer_id`, Sessions.guestengineer_id AS `Sessions__guestengineer_id`, Sessions.date_start AS `Sessions__date_start`, Sessions.date_end AS `Sessions__date_end`, Sessions.starttime AS `Sessions__starttime`, Sessions.hours AS `Sessions__hours`, Sessions.session_genre AS `Sessions__session_genre`, Sessions.no_people AS `Sessions__no_people`, Sessions.studio_usage AS `Sessions__studio_usage`, Sessions.otherpeople_req AS `Sessions__otherpeople_req`, Sessions.special_req AS `Sessions__special_req`, Sessions.created AS `Sessions__created`, Sessions.modified AS `Sessions__modified`, Studios.id AS `Studios__id`, Studios.name AS `Studios__name`, Studios.description AS `Studios__description`, Studios.created AS `Studios__created`, Studios.modified AS `Studios__modified`, Engineers.id AS `Engineers__id`, Engineers.user_id AS `Engineers__user_id`, Engineers.eng_firstname AS `Engineers__eng_firstname`, Engineers.eng_lastname AS `Engineers__eng_lastname`, Engineers.eng_email AS `Engineers__eng_email`, Engineers.eng_phoneno AS `Engineers__eng_phoneno`, Engineers.eng_status AS `Engineers__eng_status`, Engineers.rate AS `Engineers__rate`, Engineers.created AS `Engineers__created`, Engineers.modified AS `Engineers__modified`, Artists.id AS `Artists__id`, Artists.name AS `Artists__name`, Artists.cp_id AS `Artists__cp_id`, Artists.user_id AS `Artists__user_id`, Artists.genre AS `Artists__genre`, Artists.created AS `Artists__created`, Artists.modified AS `Artists__modified` FROM bookings Bookings LEFT JOIN sessions Sessions ON Bookings.id = (Sessions.booking_id) INNER JOIN studios Studios ON Studios.id = (Sessions.studio_id) LEFT JOIN engineers Engineers ON Engineers.id = (Sessions.engineer_id) INNER JOIN artists Artists ON Artists.id = (Bookings.artist_id) WHERE (status = :c0 AND Sessions.date_end &lt; :c1) ORDER BY Bookings.created ASC',
    'params' =&gt; [
        ':c0' =&gt; [
            'value' =&gt; 'confirmed',
            'type' =&gt; 'string',
            'placeholder' =&gt; 'c0'
        ],
        ':c1' =&gt; [
            'value' =&gt; '20/2/17, 4:34 p02',
            'type' =&gt; null,
            'placeholder' =&gt; 'c1'
        ]
    ],
    'defaultTypes' =&gt; [
        'Bookings__id' =&gt; 'integer',
        'Bookings.id' =&gt; 'integer',
        'id' =&gt; 'integer',
        'Bookings__artist_id' =&gt; 'integer',
        'Bookings.artist_id' =&gt; 'integer',
        'artist_id' =&gt; 'integer',
        'Bookings__status' =&gt; 'string',
        'Bookings.status' =&gt; 'string',
        'status' =&gt; 'string',
        'Bookings__amount' =&gt; 'decimal',
        'Bookings.amount' =&gt; 'decimal',
        'amount' =&gt; 'decimal',
        'Bookings__created' =&gt; 'datetime',
        'Bookings.created' =&gt; 'datetime',
        'created' =&gt; 'datetime',
        'Bookings__modified' =&gt; 'datetime',
        'Bookings.modified' =&gt; 'datetime',
        'modified' =&gt; 'datetime',
        'Sessions__id' =&gt; 'integer',
        'Sessions.id' =&gt; 'integer',
        'Sessions__booking_id' =&gt; 'integer',
        'Sessions.booking_id' =&gt; 'integer',
        'booking_id' =&gt; 'integer',
        'Sessions__studio_id' =&gt; 'integer',
        'Sessions.studio_id' =&gt; 'integer',
        'studio_id' =&gt; 'integer',
        'Sessions__engineer_id' =&gt; 'integer',
        'Sessions.engineer_id' =&gt; 'integer',
        'engineer_id' =&gt; 'integer',
        'Sessions__guestengineer_id' =&gt; 'integer',
        'Sessions.guestengineer_id' =&gt; 'integer',
        'guestengineer_id' =&gt; 'integer',
        'Sessions__date_start' =&gt; 'date',
        'Sessions.date_start' =&gt; 'date',
        'date_start' =&gt; 'date',
        'Sessions__date_end' =&gt; 'date',
        'Sessions.date_end' =&gt; 'date',
        'date_end' =&gt; 'date',
        'Sessions__starttime' =&gt; 'time',
        'Sessions.starttime' =&gt; 'time',
        'starttime' =&gt; 'time',
        'Sessions__hours' =&gt; 'integer',
        'Sessions.hours' =&gt; 'integer',
        'hours' =&gt; 'integer',
        'Sessions__session_genre' =&gt; 'text',
        'Sessions.session_genre' =&gt; 'text',
        'session_genre' =&gt; 'text',
        'Sessions__no_people' =&gt; 'integer',
        'Sessions.no_people' =&gt; 'integer',
        'no_people' =&gt; 'integer',
        'Sessions__studio_usage' =&gt; 'text',
        'Sessions.studio_usage' =&gt; 'text',
        'studio_usage' =&gt; 'text',
        'Sessions__otherpeople_req' =&gt; 'text',
        'Sessions.otherpeople_req' =&gt; 'text',
        'otherpeople_req' =&gt; 'text',
        'Sessions__special_req' =&gt; 'text',
        'Sessions.special_req' =&gt; 'text',
        'special_req' =&gt; 'text',
        'Sessions__created' =&gt; 'datetime',
        'Sessions.created' =&gt; 'datetime',
        'Sessions__modified' =&gt; 'datetime',
        'Sessions.modified' =&gt; 'datetime',
        'Studios__id' =&gt; 'integer',
        'Studios.id' =&gt; 'integer',
        'Studios__name' =&gt; 'string',
        'Studios.name' =&gt; 'string',
        'name' =&gt; 'string',
        'Studios__description' =&gt; 'text',
        'Studios.description' =&gt; 'text',
        'description' =&gt; 'text',
        'Studios__created' =&gt; 'datetime',
        'Studios.created' =&gt; 'datetime',
        'Studios__modified' =&gt; 'datetime',
        'Studios.modified' =&gt; 'datetime',
        'Engineers__id' =&gt; 'integer',
        'Engineers.id' =&gt; 'integer',
        'Engineers__user_id' =&gt; 'integer',
        'Engineers.user_id' =&gt; 'integer',
        'user_id' =&gt; 'integer',
        'Engineers__eng_firstname' =&gt; 'string',
        'Engineers.eng_firstname' =&gt; 'string',
        'eng_firstname' =&gt; 'string',
        'Engineers__eng_lastname' =&gt; 'string',
        'Engineers.eng_lastname' =&gt; 'string',
        'eng_lastname' =&gt; 'string',
        'Engineers__eng_email' =&gt; 'string',
        'Engineers.eng_email' =&gt; 'string',
        'eng_email' =&gt; 'string',
        'Engineers__eng_phoneno' =&gt; 'integer',
        'Engineers.eng_phoneno' =&gt; 'integer',
        'eng_phoneno' =&gt; 'integer',
        'Engineers__eng_status' =&gt; 'string',
        'Engineers.eng_status' =&gt; 'string',
        'eng_status' =&gt; 'string',
        'Engineers__rate' =&gt; 'decimal',
        'Engineers.rate' =&gt; 'decimal',
        'rate' =&gt; 'decimal',
        'Engineers__created' =&gt; 'datetime',
        'Engineers.created' =&gt; 'datetime',
        'Engineers__modified' =&gt; 'datetime',
        'Engineers.modified' =&gt; 'datetime',
        'Artists__id' =&gt; 'integer',
        'Artists.id' =&gt; 'integer',
        'Artists__name' =&gt; 'string',
        'Artists.name' =&gt; 'string',
        'Artists__cp_id' =&gt; 'integer',
        'Artists.cp_id' =&gt; 'integer',
        'cp_id' =&gt; 'integer',
        'Artists__user_id' =&gt; 'integer',
        'Artists.user_id' =&gt; 'integer',
        'Artists__genre' =&gt; 'text',
        'Artists.genre' =&gt; 'text',
        'genre' =&gt; 'text',
        'Artists__created' =&gt; 'datetime',
        'Artists.created' =&gt; 'datetime',
        'Artists__modified' =&gt; 'datetime',
        'Artists.modified' =&gt; 'datetime'
    ],
    'decorators' =&gt; (int) 0,
    'executed' =&gt; false,
    'hydrate' =&gt; true,
    'buffered' =&gt; true,
    'formatters' =&gt; (int) 0,
    'mapReducers' =&gt; (int) 0,
    'contain' =&gt; [
        'Sessions' =&gt; [
            'Studios' =&gt; [],
            'Engineers' =&gt; []
        ],
        'Artists' =&gt; []
    ],
    'matching' =&gt; [],
    'extraOptions' =&gt; [],
    'repository' =&gt; object(App\Model\Table\BookingsTable) {

        'registryAlias' =&gt; 'Bookings',
        'table' =&gt; 'bookings',
        'alias' =&gt; 'Bookings',
        'entityClass' =&gt; 'App\Model\Entity\Booking',
        'associations' =&gt; [
            (int) 0 =&gt; 'artists',
            (int) 1 =&gt; 'payments',
            (int) 2 =&gt; 'sessions'
        ],
        'behaviors' =&gt; [
            (int) 0 =&gt; 'Timestamp'
        ],
        'defaultConnection' =&gt; 'default',
        'connectionName' =&gt; 'default'

    }

}

更新:debug($ booking)如下,使用1条记录:

object(App\Model\Entity\Booking) {

    'id' => (int) 133,
    'artist_id' => (int) 6,
    'status' => 'confirmed',
    'amount' => '',
    'created' => object(Cake\I18n\FrozenTime) {

        'time' => '2017-02-20T16:27:15+00:00',
        'timezone' => 'UTC',
        'fixedNowTime' => false

    },
    'modified' => object(Cake\I18n\FrozenTime) {

        'time' => '2017-02-20T16:32:08+00:00',
        'timezone' => 'UTC',
        'fixedNowTime' => false

    },
    'artist' => object(App\Model\Entity\Artist) {

        'id' => (int) 6,
        'name' => 'test1',
        'cp_id' => (int) 6,
        'user_id' => (int) 25,
        'genre' => 'Anything really.',
        'created' => object(Cake\I18n\FrozenTime) {

            'time' => '2017-01-30T08:57:41+00:00',
            'timezone' => 'UTC',
            'fixedNowTime' => false

        },
        'modified' => object(Cake\I18n\FrozenTime) {

            'time' => '2017-01-30T08:57:41+00:00',
            'timezone' => 'UTC',
            'fixedNowTime' => false

        },
        '[new]' => false,
        '[accessible]' => [
            '*' => true
        ],
        '[dirty]' => [],
        '[original]' => [],
        '[virtual]' => [],
        '[errors]' => [],
        '[invalid]' => [],
        '[repository]' => 'Artists'

    },
    'session' => object(App\Model\Entity\Session) {

        'id' => (int) 100,
        'booking_id' => (int) 133,
        'studio_id' => (int) 2,
        'engineer_id' => (int) 2,
        'guestengineer_id' => null,
        'date_start' => object(Cake\I18n\FrozenDate) {

            'time' => '2017-02-28T00:00:00+00:00',
            'timezone' => 'UTC',
            'fixedNowTime' => false

        },
        'date_end' => object(Cake\I18n\FrozenDate) {

            'time' => '2017-03-14T00:00:00+00:00',
            'timezone' => 'UTC',
            'fixedNowTime' => false

        },
        'starttime' => null,
        'hours' => null,
        'session_genre' => 'tests',
        'no_people' => (int) 5,
        'studio_usage' => 'tests',
        'otherpeople_req' => '',
        'special_req' => '',
        'created' => object(Cake\I18n\FrozenTime) {

            'time' => '2017-02-20T16:27:15+00:00',
            'timezone' => 'UTC',
            'fixedNowTime' => false

        },
        'modified' => object(Cake\I18n\FrozenTime) {

            'time' => '2017-02-20T16:32:08+00:00',
            'timezone' => 'UTC',
            'fixedNowTime' => false

        },
        'engineer' => object(App\Model\Entity\Engineer) {

            'id' => (int) 2,
            'user_id' => (int) 18,
            'eng_firstname' => 'test',
            'eng_lastname' => 'test',
            'eng_email' => 'test3@email.com',
            'eng_phoneno' => (int) 256,
            'eng_status' => 'staff',
            'rate' => (float) 51,
            'created' => object(Cake\I18n\FrozenTime) {

                'time' => '2016-10-11T09:27:09+00:00',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            'modified' => object(Cake\I18n\FrozenTime) {

                'time' => '2016-10-11T09:27:09+00:00',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'Engineers'

        },
        'studio' => object(App\Model\Entity\Studio) {

            'id' => (int) 2,
            'name' => 'Studio B',
            'description' => 'Studio B',
            'created' => object(Cake\I18n\FrozenTime) {

                'time' => '2016-10-11T00:04:28+00:00',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            'modified' => object(Cake\I18n\FrozenTime) {

                'time' => '2016-10-11T00:04:28+00:00',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'Studios'

        },
        '[new]' => false,
        '[accessible]' => [
            '*' => true
        ],
        '[dirty]' => [],
        '[original]' => [],
        '[virtual]' => [],
        '[errors]' => [],
        '[invalid]' => [],
        '[repository]' => 'Sessions'

    },
    '[new]' => false,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [
        'amount' => true
    ],
    '[original]' => [
        'amount' => (float) 726.95
    ],
    '[virtual]' => [],
    '[errors]' => [],
    '[invalid]' => [],
    '[repository]' => 'Bookings'

}

1 个答案:

答案 0 :(得分:0)

我找到了问题的解决方案。在我的虚拟状态课程中,我有一个If语句=使用了值的分配($ booking-&gt; payment =&#39;&#39;)而不是比较条件($ booking-&gt;付款==&#39;&#39;),将值设置为0,因此显示为0金额,尽管原始金额不是0。