Currently I'm creating an application to manage my TO-DO activities.

In my database model I've created a table that refers to the task table, so that I can create requirements for tasks. This way I can only start/show tasks in my list whenever I completed the required tasks.

Model: the model

The functionality works perfectly, but I have a problem with the cakephp way of receiving variables in your view.

Since the parent and child id are both a [projecttasks][projecttasks_name], the last one overwrites the first, resulting in this on the page: the failed result

But when I click edit, you can see it's actually saved correctly: Correctly saved

Now, in the code it looks like this in the view:

<?php foreach ($itemrequirements as $itemrequirement): ?>
    <tr>
        <td><?php echo h($itemrequirement['Projecttask']['projecttasks_name']); ?></td>
        <td><?php echo h($itemrequirement['Projecttask']['projecttasks_name']); ?></td>
        <td class="actions">
            <?php echo $this->Html->link('View',array('action' => 'detail', $itemrequirement['Itemrequirement']['itemreq_id'])); ?>
            <?php echo $this->Html->link('Edit',array('action' => 'edit', $itemrequirement['Itemrequirement']['itemreq_id'])); ?>
            <?php echo $this->Form->Postlink('Delete',array('action' => 'delete', $itemrequirement['Itemrequirement']['itemreq_id']), 
            null, sprintf('Are you sure you want to delete %s?', $itemrequirement['Itemrequirement']['itemreq_id'])); ?>
        </td>
    </tr>
    <?php endforeach; ?>

Controller:

public function index() {

    // write redirect information for screens into session
    $this->Session->write('Redirector.controllername', 'itemrequirements');
    $this->Session->write('Redirector.controllerfunction', 'index');
    $this->Session->write('Redirector.recordindex', NULL);

    // build index
    $this -> Itemrequirement -> recursive = 0;
    $this -> set('itemrequirements', $this -> paginate());

    // get total record count
    $totalItemrequirements = $this -> Itemrequirement -> find('count');
    // expose total-count to the view
    $this -> set('totalItemrequirements', $totalItemrequirements);

}

I've asked my boss around, and he pointed this part out (Model file). Thought I'd share.

/**
 * declare BelongsTo relations
 *
 * @var array $belongsTo
 * @link http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#belongsto BelongsTo docs (CakePHP cookbook)
 */
public $belongsTo = array(
    'Projecttask' => array(
        'className' => 'Projecttask',
        'foreignKey' => 'itemreqs_rel_projectparents'
    ),
    'Projecttask' => array(
        'className' => 'Projecttask',
        'foreignKey' => 'itemreqs_rel_projectchilds'
    )
);

Is there any way I could get the view correctly working? I'm pretty new to the MVC-model and made most of my code by using the code generator we have running at work.

有帮助吗?

解决方案

Just make your model have a different name for the parent and child like this:

public $belongsTo = array(
    'ProjecttaskParent' => array(
        'className' => 'Projecttask',
        'foreignKey' => 'itemreqs_rel_projectparents'
    ),
    'ProjecttaskChild' => array(
        'className' => 'Projecttask',
        'foreignKey' => 'itemreqs_rel_projectchilds'
    )
);

Also if you have a look at cake model conventions you will see that you haven't really setup properly to summon cake's complete power of minimum configuration. It will work but you will need to do more than what you ought to.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top