CakePHP does not display modified correctly.

Asked 1 years ago, Updated 1 years ago, 50 views

topics table array image modified is not displayed correctly

I recently started studying CakePHP.
I'm suffering from an error right away...

I think it's a rudimentary problem, so
If anyone knows, please reply.

<div class="topics index">
    <h2><?phpecho__('Topics');?>/h2>
    <table cellpadding="0" cellspacing="0">
    <thead>
    <tr>
            <th><?phpecho$this->Paginator->sort('id');?>/th>
            <th><?phpecho$this->Paginator->sort('title');?>/th>
            <th><?phpecho$this->Paginator->sort('body');?>/th>
            <th><?phpecho$this->Paginator->sort('category_id');?>/th>
            <th><?phpecho$this->Paginator->sort('created');?>/th>
            <th><?phpecho$this->Paginator->sort('modified');?>/th>
            <th class="actions">>?phpecho__('Actions');?>/th>
    </tr>
    </thead>
    <tbody>
    <?php foreach($topics as$topic): ?>
    <tr>
        <td><?phpechoh($topic['Topic']['id']);?>>nbsp;</td>
        <td><?phpechoh($topic['Topic']['title']);?>>nbsp;</td>
        <td><?phpechoh($topic['Topic']['body']);?>>nbsp;</td>
        <td>
            <?php echo $this->Html->link($topic['Category']['name'], array('controller'=>'categories', 'action'='view', $topic['Category']['id']);?>
        </td>
        <td><?phpechoh($topic['Topic']['created']);?>>nbsp;</td>
        <td><?phpechoh($topic['Topic']['modified']);?>>nbsp;</td>
        <td class="actions">
            <?php echo $this->Html->link(__('View'), array('action'=>'view', $topic['Topic']['id']));?>
            <?php echo $this->Html->link(__('Edit'), array('action'=>'edit', $topic['Topic']['id']));?>
            <?php echo $this->Form->postLink(__('Delete'), array('action'=>'delete', $topic['Topic']['id']), array(),__('Are you sure you want to delete #%s?', $topic['Topic'][id']);)?
        </td>
    </tr>
<?php endforeach;?>
    </tbody>
    </table>
    <p>
    <?php
    echo$this->Paginator->counter(array(
    'format'=>__('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
    ));
    ?></p>
    <div class="paging">
    <?php
        echo$this->Paginator->prev('<'.__('previous'), array(), null, array('class'=>'prev disabled'));
        echo$this->Paginator->number(array('separator'=>'));
        echo$this->Paginator->next(__('next').'>', array(), null, array('class'=>'next disabled'));
    ?>
    </div>
</div>
<div class="actions">
    <h3><?phpecho__('Actions');?>/h3>
    <ul>
        <li><?phpecho$this->Html->link(__('New Topic'), array('action'=>'add'));?>>/li>
        <li><?phpecho$this->Html->link(__('List Categories'), array('controller'=>'categories', 'action'=>'index'));?>/li>
        <li><?phpecho$this->Html->link(__('New Category'), array('controller'=>'categories', 'action'=>'add'));?>>/li>
        <li><?phpecho$this->Html->link(__('List Comments'), array('controller'=>'comments', 'action'=>'index'));?>></li>
        <li><?phpecho$this->Html->link(__('New Comment'), array('controller'=>'comments', 'action'=>'add'));?>></li>
    </ul>
</div>
<?php
App::uses('AppController', 'Controller');
/**
 * Topics Controller
 *
 * @propertyTopic $Topic
 * @propertyPaginatorComponent$Paginator
 */
class TopicsController extensions AppController {

/**
 * Components
 *
 * @var array
 */
    public$components=array('Paginator');

/**
 * index method
 *
 * @return void
 */
    public function index() {
        $this->Topic->recursive=0;
        $this->set('topics', $this->Paginator->paginate());
    }

/**
 * view method
 *
 * @throws NotFoundException
 * @param string$id
 * @return void
 */
    public function view($id=null){
        if(!$this->Topic->exists($id)){
            through new NotFoundException(__('Invalid topic'));
        }
        $options=array('conditions'=>array('Topic.'.$this->Topic->primaryKey=>$id));
        $this->set('topic', $this->Topic->find('first',$options));
    }

/**
 * add method
 *
 * @return void
 */
    public function add() {
        if($this->request->is('post'))){
            $this->Topic->create();
            if($this->Topic->save($this->request->data)){
                $this->Session->setFlash(__('The topic has been saved.');
                return $this->redirect(array('action'=>'index'));
            } else{
                $this->Session->setFlash(__('The topic could not be saved. Please, try again.'));
            }
        }
        $categories=$this->Topic->Category->find('list');
        $this->set(compact('categories'));
    }

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string$id
 * @return void
 */
    public function edit($id=null){
        if(!$this->Topic->exists($id)){
            through new NotFoundException(__('Invalid topic'));
        }
        if($this->request->is(array('post','put')){
            if($this->Topic->save($this->request->data)){
                $this->Session->setFlash(__('The topic has been saved.');
                return $this->redirect(array('action'=>'index'));
            } else{
                $this->Session->setFlash(__('The topic could not be saved. Please, try again.'));
            }
        } else{
            $options=array('conditions'=>array('Topic.'.$this->Topic->primaryKey=>$id));
            $this->request->data=$this->Topic->find('first',$options);
        }
        $categories=$this->Topic->Category->find('list');
        $this->set(compact('categories'));
    }

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string$id
 * @return void
 */
    public function delete($id=null){
        $this->Topic->id=$id;
        if(!$this->Topic->exists()){
            through new NotFoundException(__('Invalid topic'));
        }
        $this->request->allowMethod('post','delete');
        if($this->Topic->delete()){
            $this->Session->setFlash(__('The topic has been deleted.');
        } else{
            $this->Session->setFlash(__('The topic could not be deleted. Please, try again.'));
        }
        return $this->redirect(array('action'=>'index'));
    }
}
<?php
App::uses('AppModel', 'Model');
/**
 * Topic Model
 *
 * @property Category $Category
 * @property Comment $Comment
 */
class Topic extensions AppModel {

/**
 * 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
            ),
        ),
        US>'body'=>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
            ),
        ),
        'category_id' = > array(
            'numeric' = > array(
                'rule' = > array('numeric'),
                // '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
            ),
        ),
        'modified' = > array(
            'datetime' = > array(
                'rule' = > array('datetime'),
                // '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, that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
    public$belongsTo=array(
        US>'Category' = > array(
            'className' = > 'Category',
            'foreignKey' = > 'category_id',
            'conditions' = >',
            'fields' = >',
            'order' = >'
        )
    );

/**
 * hasMany associations
 *
 * @var array
 */
    public$hasMany=array(
        'Comment' = > array(
            'className' = > 'Comment',
            'foreignKey' = > 'topic_id',
            'dependent' = > false,
            'conditions' = >',
            'fields' = >',
            'order' = >',
            'limit' = >',
            'offset' = >',
            'exclusive' = >',
            'finderQuery' = >',
            'counterQuery' = >'
        )
    );

}

php mysql cakephp

2022-09-30 10:31

1 Answers

This answer is specific to the Comment.
However, @DaikiYamada has already deleted the cache file and the symptom has not been resolved, so it seems that the following measures have not been resolved.
It's hard to understand if it's a comment, so I'll write it down as an answer.
This may not apply if you have changed the settings independently in App/Config/core.php.

App/Config/core.php:$engine
App/Config/core.php:$duration

If $engine is 'File', the DB information is stored below App/tmp/cache/models/.

According to @Daiki Yamada's condition, I think it is as follows.

  • myapp_cake_model_default_samples_myfriends_categories
  • myapp_cake_model_default_samples_myfriends_comments
  • myapp_cake_model_default_samples_myfriends_list
  • myapp_cake_model_default_samples_myfriends_topics

The *_list file contains a list of tables stored in the samples_myfriends database.

*_(db name) files contain information for each table.

For example, if you add a modified column in the middle, the cache still caches a table in which the modified column does not exist.
Describes what to do with the modified column before the cache is updated.
If the model or controller implements code that assumes modified exists, it can cause inconsistencies by retrieving table information from the cache above, and may act like modified does not exist in the DB table.

This will no longer cause the file to be cached, so the table information will no longer be reflected.

// It's difficult to explain in words, I tried the code you gave me in my environment (CentOS, CakePHP 2.6.1, PHP 5.6.4) and I was able to execute it without any problems.
// If you really can't do anything about it, it might be a good idea to rebuild your environment.


2022-09-30 10:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.