A
A
Alexander2014-04-15 09:41:35
symfony
Alexander, 2014-04-15 09:41:35

Symfony2 - how to check if an object is a specific entity?

How can such a check be done in symfony2?
There are such objects that come from the form:

Project\MediaBundle\Entity\File Object
(
    [id:Project\MediaBundle\Entity\File:private] => 
    [name:Project\MediaBundle\Entity\File:private] => Symfony\Component\HttpFoundation\File\UploadedFile Object
        (
            [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
            [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 2014-04-09 00-25-51.JPG
            [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
            [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 32347
            [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
            [pathName:SplFileInfo:private] => /tmp/phpBvJQSn
            [fileName:SplFileInfo:private] => phpBvJQSn
        )

    [size:Project\MediaBundle\Entity\File:private] => 
    [description:Project\MediaBundle\Entity\File:private] => описание картинки
    [type:Project\MediaBundle\Entity\File:private] => 
)

Project\CurBundle\Entity\Prop Object
(
    [id:Project\CurBundle\Entity\Prop:private] => 
    [value:Project\CurBundle\Entity\Prop:private] => первый текст
    [element:Project\CurBundle\Entity\Prop:private] => 
    [property:Project\CurBundle\Entity\Prop:private] => 
    [property_id:Project\CurBundle\Entity\Prop:private] => 
)

Those. two objects Project\MediaBundle\Entity\File and Project\CurBundle\Entity\Prop . What these objects are is not known in advance. They can be any (from a certain set).
Is it possible to check something like:
if (isEntityObject('Project\MediaBundle\Entity\File')) {
    echo 'Это объект из нашего проекта типа "файл"';
}
if (isEntityObject('Project\CurBundle\Entity\Prop')) {
    echo 'Это объект из нашего проекта типа "свойство"';
}

Or it can be done simply by means of php, something I don't know :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2014-04-15
@neokortex

If your class is under the namespace Entity, then it should be Entity, if it is not, then you need to take these classes out of this namespace.
Entity in the context of Symfony is an ordinary class (we do not take into account the fact that when it is selected from the base, it turns into a proxy). If you need to check if a variable is a class instance, then use instanceof.
ps storing UploadedFile in Entity is not the best solution.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question