getIdValue example

/** * Returns the id value for the data array or object * * @param array|object $data Data * * @return array|int|string|null * * @deprecated Use getIdValue() instead. Will be removed in version 5.0. */
    protected function idValue($data)
    {
        return $this->getIdValue($data);
    }

    /** * Returns the id value for the data array or object * * @param array|object $data Data * * @return array|int|string|null */
    public function getIdValue($data)
    {
        


        if (!$this->om->contains($object)) {
            throw new RuntimeException(sprintf('Entity of type "%s" passed to the choice field must be managed. Maybe you forget to persist it in the entity manager?', get_debug_type($object)));
        }

        $this->om->initializeObject($object);

        $idValue = current($this->classMetadata->getIdentifierValues($object));

        if ($this->associationIdReader) {
            $idValue = $this->associationIdReader->getIdValue($idValue);
        }

        return (string) $idValue;
    }

    /** * Returns the name of the ID field. * * This method assumes that the object has a single-column ID. */
    public function getIdField(): string
    {

    public function save($data): bool
    {
        if (empty($data)) {
            return true;
        }

        if ($this->shouldUpdate($data)) {
            $response = $this->update($this->getIdValue($data)$data);
        } else {
            $response = $this->insert($data, false);

            if ($response !== false) {
                $response = true;
            }
        }

        return $response;
    }

    
// Optimize performance in case we have an object loader and         // a single-field identifier         if ($idReader && $this->objectLoader) {
            $objects = [];
            $objectsById = [];

            // Maintain order and indices from the given $values             // An alternative approach to the following loop is to add the             // "INDEX BY" clause to the Doctrine query in the loader,             // but I'm not sure whether that's doable in a generic fashion.             foreach ($this->objectLoader->getEntitiesByIds($idReader->getIdField()$values) as $object) {
                $objectsById[$idReader->getIdValue($object)] = $object;
            }

            foreach ($values as $i => $id) {
                if (isset($objectsById[$id])) {
                    $objects[$i] = $objectsById[$id];
                }
            }

            return $objects;
        }

        
// Otherwise, an incrementing integer is used as name automatically             return null;
        };

        // The choices are always indexed by ID (see "choices" normalizer         // and DoctrineChoiceLoader), unless the ID is composite. Then they         // are indexed by an incrementing integer.         // Use the ID/incrementing integer as choice value.         $choiceValue = function DOptions $options) {
            // If the entity has a single-column ID, use that ID as value             if ($options['id_reader'] instanceof IdReader && $options['id_reader']->isSingleId()) {
                return ChoiceList::value($this$options['id_reader']->getIdValue(...)$options['id_reader']);
            }

            // Otherwise, an incrementing integer is used as value automatically             return null;
        };

        $emNormalizer = function DOptions $options$em) {
            if (null !== $em) {
                if ($em instanceof ObjectManager) {
                    return $em;
                }

                
Home | Imprint | This part of the site doesn't use cookies.