Announcement
- Index
- Thelia 2 - Section française
- Développement
- Comment récupérer les valeurs d'une table dans une boucle étendue.
#1 Comment récupérer les valeurs d'une table dans une boucle étendue.
(28-05-2018 08:55:55)
J'ai réussi à étendre la boucle "product". Super !
....
$Array_id = $event->getLoop()->getAmpSellerId();
if (!empty($Array_id)) {
$id = $Array_id[0];
$ampSellerJoin = new Join(ProductTableMap::ID, AmpSellerProductTableMap::PRODUCT_ID, Criteria::LEFT_JOIN);
$event->getModelCriteria()->addJoinObject($ampSellerJoin, "ampSellerJoin")->where(AmpSellerProductTableMap::AMP_SELLER_ID . " " . Criteria::IN . " (" . $id . ")");
}
....
Maintenant, j'aimerais afficher les infos de la table jointe.
Pour le moment, j'arrive à récupéré l'id du seller :
if ($arguments->get('amp_seller_id')->getValue()) {
foreach ($loopResult as $row) {
$amp_seller_id_array = $arguments->get('amp_seller_id')->getValue();
$amp_seller_id = $amp_seller_id_array[0];
$row->set('ID_AMP_SELLER', $amp_seller_id);
}
}
Ce code semble bon, lorsqu'il y a un seul id, mais à voir avec plusieurs.
Et surtout comment faire pour récupérer les infos de la table joint ?
Merci d'avance de vos éclaircissements.
#2 Re: Comment récupérer les valeurs d'une table dans une boucle étendue.
(29-05-2018 13:16:17)
Bon, je m'y suis pris autrement. Tout se passe dans la méthode "productParseResults"
Voici mon code
ArtabanMarketplace\EventListeners\Loop.php
<?php
/**
* Extends Loops
*
* Class Loop
* @author Gilles Lengy
*/
namespace ArtabanMarketplace\EventListeners;
use ArtabanMarketplace\Model\Map\AmpSellerProductVersionTableMap;
use ArtabanMarketplace\Model\AmpSellerProductQuery;
use ArtabanMarketplace\Model\AmpSellerVersionQuery;
use ArtabanMarketplace\Model\AmpSellerI18nQuery;
use ArtabanMarketplace\Model\AmpSeller;
use Thelia\Model\Map\ProductTableMap;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Core\Event\Loop\LoopExtendsArgDefinitionsEvent;
use Thelia\Core\Template\Loop\Argument\Argument;
use Thelia\Core\Event\Loop\LoopExtendsInitializeArgsEvent;
//use Thelia\Core\EventListeners\LoopExtendsBuildModelCriteriaEvent as LoopExtendsBuildModelCriteriaEventListeners; // !!!!!!!!!!!!!!!!!!!! ATTENTION ARTABAN GL :::: Vraiment utile ? Apparement non. Ca à l'air de marcher sans.
use Thelia\Core\Event\Loop\LoopExtendsBuildModelCriteriaEvent;
use Propel\Runtime\ActiveQuery\Join;
use Propel\Runtime\ActiveQuery\Criteria;
use Thelia\Core\Event\Loop\LoopExtendsParseResultsEvent;
class Loop implements EventSubscriberInterface {
/** @var Request $request */
protected $request = null;
public function __construct(Request $request) {
$this->request = $request;
}
/**
* @inheritdoc
*/
public static function getSubscribedEvents() {
return [
// Product
TheliaEvents::getLoopExtendsEvent(TheliaEvents::LOOP_EXTENDS_ARG_DEFINITIONS, 'product') => ['productArgDefinitions', 128],
TheliaEvents::getLoopExtendsEvent(TheliaEvents::LOOP_EXTENDS_INITIALIZE_ARGS, 'product') => ['productInitializeArgs', 128],
TheliaEvents::getLoopExtendsEvent(TheliaEvents::LOOP_EXTENDS_BUILD_MODEL_CRITERIA, 'product') => ['productBuildModelCriteria', 128],
TheliaEvents::getLoopExtendsEvent(TheliaEvents::LOOP_EXTENDS_PARSE_RESULTS, 'product') => ['productParseResults', 128],
];
}
/**
* Add a new parameter for loop lang
* you can now call the loop with this argument
*/
public function productArgDefinitions(LoopExtendsArgDefinitionsEvent $event) {
$argument = $event->getArgumentCollection();
$argument->addArgument(Argument::createIntListTypeArgument('amp_seller_id'));
}
/**
* Change the query search of the loop product
*/
public function productBuildModelCriteria(LoopExtendsBuildModelCriteriaEvent $event) {
$Array_id = $event->getLoop()->getAmpSellerId();
if (!empty($Array_id)) {
$id = $Array_id[0];
$ampProductSellerJoin = new Join(ProductTableMap::ID, AmpSellerProductVersionTableMap::PRODUCT_ID, Criteria::LEFT_JOIN);
$event->getModelCriteria()
->addJoinObject($ampProductSellerJoin, "ampSellerProductJoin")
->where(AmpSellerProductVersionTableMap::AMP_SELLER_ID . " " . Criteria::IN . " (" . $id . ")");
}
}
/**
* Add the output variables of the loop product
*/
public function productParseResults(LoopExtendsParseResultsEvent $event) {
$loopResult = $event->getLoopResult();
foreach ($loopResult as $row) {
$query_product = new AmpSellerProductQuery();
$result_query_array_product = $query_product->filterByProductId($row->get('ID'))->find();
if($result_query_array_product[0]){
$amp_seller_id = $result_query_array_product[0]->getAmpSellerId();
}
if ($amp_seller_id > 0) {
$query_amp_seller = new AmpSellerVersionQuery();
$result_query_array_amp_seller = $query_amp_seller->filterById($amp_seller_id)->find();
$amp_seller = $result_query_array_amp_seller[1];
$row->set('ADDRESS1', $amp_seller->getAddress1());
$row->set('ID_AMP_SELLER', $amp_seller->getID());
$query_amp_sellerI18 = new AmpSellerI18nQuery;
$result_query_array_amp_sellerI18 = $query_amp_sellerI18->filterById($amp_seller->getID())->find();
$amp_sellerI18 = $result_query_array_amp_sellerI18[0];
$row->set('TITLE_SELLER', $amp_sellerI18->getTitle());
}
}
}
/**
* Set parameters from the query string
*/
public function productInitializeArgs(LoopExtendsInitializeArgsEvent $event) {
$parameters = $event->getLoopParameters();
if ($this->request->query->has('loop-amp_seller_id')) {
$parameters['amp_seller_id'] = 1; // ARTABAN GL 'booléen'
$event->setLoopParameters($parameters);
}
}
}
et une boucle qui affiche bien les infos
<ul>
{loop type="product" name="my_product_loop" amp_seller_id=$seller_id order="min_price"}
<li>{$ID} / {$TITLE} ({$REF}) > id vendeur : {$ID_AMP_SELLER} > Nom vendeur : {$TITLE_SELLER} > Adresse 1 : {$ADDRESS1}</li>
{/loop}
</ul>
A ceux qui savent, est-ce que cela vous semble être la bonne méthode ?
Mes d'avance de vos réponses.
- Index
- Thelia 2 - Section française
- Développement
- Comment récupérer les valeurs d'une table dans une boucle étendue.