Domanda

Ho una collezione in un metodo dove voglio eseguire due operazioni diverse in questa collezione. Quindi, voglio due copie separate di stessa collezione, e quindi assegnare una delle due collezioni per collezione originale di nuovo e restituirlo.

Per fare questa semplice, supponiamo di avere una collezione oggetto chiamato $collection.

Ora, sto cercando con PHP clonazione come io non so se c'è qualche Magento raccolta clonazione integrato oppure no.

$coll1 = clone $collection;
$coll2 = clone $collection;

Ora sto cercando di eseguire diverse operazioni su questi due cloni separati della collezione originale, qualcosa di simile.

$coll1->getSelect()->where('some where condition');
$coll2->getSelect()->where('some different where condition');
if($coll1->count() == 0) {
    $collection = $coll2;
} else {
    $collection = $coll1;
}

Ma la cosa strana è, entrambi questi insiemi clonati hanno sia la dove le condizioni assegnati! $ Condizione coll1 viene applicata a $ coll2 insieme a condizione di $ coll2, e viceversa.

Qualcuno sa come raggiungere questo obiettivo?

Grazie!

È stato utile?

Soluzione

Use of the PHP clone operator, where deep cloning is desired, requires classes which store objects on properties implement a __clone method to copy the objects. If they don't define it, the properties on both instances will reference the same object.

Magento does not implement __clone on it's collection abstracts, and therefore does not support deep cloning as you want it to.

My suggestion is to look for other ways to accomplish what you are wanting to do, as cloning a collection could be pretty expensive.

The example you gave (for instance) could be changed to clone the select, modify it to select a count of the records it would have loaded and then based on that result modify the collection. This would also perform better since you would not be loading a collection and counting it just to determine which one to use.

EDIT: The following demonstrates how to grab a count without loading or actually modifying the collection.

$collection = Mage::getModel(...)->getCollection();

$count = $collection->getSelectCountSql();
$count->where('some where condition');
if ($count->query()->fetchColumn() == 0) {
    ...
} else {
    ...
}

Altri suggerimenti

To expand on @davidalger's answer, you can reset the select if you want to do a different operation than a count - like so:

$select= $collection->getSelectCountSql()->reset();

$select
    ->from('newsletter_subscriber', array('some_column'))
    ->distinct();

Be careful though, this could have detrimental effects later in the process since this modifies the collection.

A better way would be to clone the select somehow, but a shallow copy wouldn't cut it since the object contains complex types (Varien_Db_Select nor Zend_Db_Select have a __clone method).

One way to get around this is to save the select data, modify it, run your query, then put back the original select data.

See here for an example: https://ka.lpe.sh/2013/05/23/magento-clone-collection-how-to-clone-collection-in-magento/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top