src/Cocorico/CoreBundle/Model/Manager/ListingImportManager.php
/**
     * Create categories and field values while listing deposit.
     *
     * @param  Listing $listing
     * @param  array   $categories Id(s) of ListingCategory(s) selected
     * @param  array   $values     Value(s) of ListingCategoryFieldValue(s) of the ListingCategory(s) selected
     *
     * @return Listing
     */
Create categories and field values while listing deposit.

@return Listing
Create categories and field values while listing deposit . @return Listing
public function addCategories(Listing $listing, array $categories, array $values)
    {
        foreach ($categories as $i => $category) {
            $listingCategory = $this->em->getRepository('CocoricoCoreBundle:ListingCategory')->findOneById($category);
            $listingListingCategory = new ListingListingCategory();
            $listingListingCategory->setListing($listing);
            $listingListingCategory->setCategory($listingCategory);
            if ($listingCategory->getFields()) {
                foreach ($listingCategory->getFields() as $field) {
                    $fieldValue = new \Cocorico\ListingCategoryFieldBundle\Entity\ListingCategoryFieldValue();
                    $fieldValue->setListingListingCategory($listingListingCategory);
                    $fieldValue->setListingCategoryListingCategoryField($field);
                    $value = isset($values[$field->getId()]["value"]) ? $values[$field->getId()]["value"] : null;
                    $fieldValue->setValue($value);
                    $listingListingCategory->addValue($fieldValue);
                }
            }
            $listing->addListingListingCategory($listingListingCategory);
        }
        return $listing;
    }
public function addCategories ( Listing $ listing , array $ categories , array $ values ) { foreach ( $ categories as $ i => $ category ) { $ listingCategory = $ this -> em -> getRepository ( 'CocoricoCoreBundle:ListingCategory' ) -> findOneById ( $ category ) ; $ listingListingCategory = new ListingListingCategory ( ) ; $ listingListingCategory -> setListing ( $ listing ) ; $ listingListingCategory -> setCategory ( $ listingCategory ) ; if ( $ listingCategory -> getFields ( ) ) { foreach ( $ listingCategory -> getFields ( ) as $ field ) { $ fieldValue = new \ Cocorico \ ListingCategoryFieldBundle \ Entity \ ListingCategoryFieldValue ( ) ; $ fieldValue -> setListingListingCategory ( $ listingListingCategory ) ; $ fieldValue -> setListingCategoryListingCategoryField ( $ field ) ; $ value = isset ( $ values [ $ field -> getId ( ) ] [ " value " ] ) ? $ values [ $ field -> getId ( ) ] [ " value " ] : null ; $ fieldValue -> setValue ( $ value ) ; $ listingListingCategory -> addValue ( $ fieldValue ) ; } } $ listing -> addListingListingCategory ( $ listingListingCategory ) ; } return $ listing ; }
Create categories and field values while listing deposit.
Create categories and field values while listing deposit .
//Find the ListingCategory entity selected /** @var ListingCategory $listingCategory */ //Create the corresponding ListingListingCategory //Create the corresponding ListingCategoryFieldValue(s) /** @var ListingCategoryListingCategoryFieldInterface $field */ /** @var ListingCategoryFieldValueInterface $fieldValue */ //Set the values. Index of $values corresponds to the ListingCategoryListingCategoryField Id. //It permits to associate the field value with the corresponding field. //Mainly use to remove block fields when the corresponding category is unselected //See Listing->getCategoriesFieldsValuesOrderedByGroup method