PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 21, 2022

[FIXED] how to use countercache for different alias in cakephp model?

 April 21, 2022     cakephp, cakephp-2.3     No comments   

Issue

I have the following models: Attachment and PurchaseOrder hence the datatables attachments and purchase_orders.

In PurchaseOrder, I have

class PurchaseOrder extends AppModel {
public $hasMany = array(
    'Pdf' => array(
        'className'  => 'Attachment',
        'foreignKey' => 'foreign_key',
        'conditions' => array(
            'Pdf.model' => 'PurchaseOrder'
        )
    ),
    'Zip' => array(
        'className'  => 'Attachment',
        'foreignKey' => 'foreign_key',
        'conditions' => array(
            'Zip.model' => 'PurchaseOrder'
        )
    ),

In Attachment, I have the following:

public $belongsTo = array(
    'PurchaseOrder' => array(
        'className' => 'PurchaseOrder',
        'foreignKey' => 'foreign_key',
        'counterCache' => array(
            'attachment_count' => array('Pdf.model' => 'PurchaseOrder'),
        )
    ),

My problem is when I try to use $this->PurchaseOrder->Zip->save($data); I run into problem because the alias Pdf is not found.

How do I overcome this while maintaining the countercache behavior of updating the attachment_count inside purchase_orders?

Note that if a PurchaseOrder is associated with 3 Pdf Attachments and 2 Zip Attachments, the attachment_count should read 3.

I am using cakephp 2.4.2


Solution

I stopped using counterCache and used afterSave instead.

 /**
  * Called after each successful save operation.
  *
  * @param boolean $created True if this save created a new record
  * @param array $options Options passed from Model::save().
  * @return void
  * @link http://book.cakephp.org/2.0/en/models/callback-methods.html#aftersave
  * @see Model::save()
  */
public function afterSave($created, $options = array()) {
    // update the PurchaseOrder based on pdf
    if (isset($this->data['Pdf'])) {
        $this->updatePurchaseOrderAttachmentCount($this->data);
    }
}

/**
 *
 * @param Array $data where we expect key Pdf
 */
public function updatePurchaseOrderAttachmentCount($data) {
    // check for Pdf
    $pdfSet = isset($data['Pdf']);
    if (!$pdfSet) {
        throw new Exception('we expect Pdf as a key in your $data');
    }
    // check for foreign_key and model
    $pdfFKSet = isset($data['Pdf']['foreign_key']);
    $pdfModelSet = isset($data['Pdf']['model']);
    if (!$pdfFKSet || !$pdfModelSet) {
        throw new Exception('we expect foreign_key and model as keys in your $data["Pdf"]');
    }
    $count = $this->find('count', array(
        'conditions' => array(
            'model' => 'PurchaseOrder',
            'type' => 'application/pdf',
            'foreign_key' => $data['Pdf']['foreign_key'],
        )
    ));
    if (!is_numeric($count)) {
        throw new Exception('we expect numeric in the $count');
    }
    $poData = array(
        'PurchaseOrder' => array(
            'id' => $data['Pdf']['foreign_key'],
            'attachment_count' => $count,
        )
    );
    return $this->PurchaseOrder->save($poData);
}


Answered By - Kim Stacks
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing