Laravel学说只返回findAll()查询中的单个属性

时间:2017-02-03 07:38:45

标签: php mysql laravel doctrine-orm doctrine

我在流明应用程序中使用laravel doctrine,在我的一个控制器中,我有一个索引函数,它应该返回给定实体的所有记录。

该功能看起来像这样

public function index(ServerRequestInterface $request)
{
    return $this->showResponse(app()->make('em')->getRepository('App\Entities\showOff')->findAll());
}

返回这样的数据

[
    {
      "nodeType": 'showOff'
    },
    {
      "nodeType": 'showOff'
    },
    {
      "nodeType": 'showOff'
    },
    {
      "nodeType": 'showOff'
    }
]

这只是实体中的一个属性。

如果我打开doctrine调试器,则会看到执行的SQL查询,如

SELECT t0.type AS type_1, t0.size AS size_2, t0.last_modified AS last_modified_3, t0.hair_cutter AS hair_cutter_4, t0.file_path AS file_path_5, t0.content_url AS content_url_6, t0.embed_url AS embed_url_7, t0.height AS height_8, t0.width AS width_9, t0.player_type AS player_type_10, t0.about AS about_11, t0.award AS award_12, t0.comment AS comment_13, t0.comment_count AS comment_count_14, t0.text AS text_15, t0.thumbnail AS thumbnail_16, t0.version AS version_17, t0.name AS name_18, t0.id AS id_19, t0.nid AS nid_20, t0.node_type AS node_type_21, t0.owner_id AS owner_id_22, t23.enabled AS enabled_24, t23.username AS username_25, t23.email AS email_26, t23.password AS password_27, t23.remember_token AS remember_token_28, t23.name AS name_29, t23.id AS id_30, t23.nid AS nid_31, t23.node_type AS node_type_32, t0.aggregate_rating_id AS aggregate_rating_id_33, t34.rating_count AS rating_count_35, t34.rating_score AS rating_score_36, t34.name AS name_37, t34.id AS id_38, t34.nid AS nid_39, t34.node_type AS node_type_40, t0.author_id AS author_id_41, t42.enabled AS enabled_43, t42.username AS username_44, t42.email AS email_45, t42.password AS password_46, t42.remember_token AS remember_token_47, t42.name AS name_48, t42.id AS id_49, t42.nid AS nid_50, t42.node_type AS node_type_51, t0.translator_id AS translator_id_52, t53.enabled AS enabled_54, t53.username AS username_55, t53.email AS email_56, t53.password AS password_57, t53.remember_token AS remember_token_58, t53.name AS name_59, t53.id AS id_60, t53.nid AS nid_61, t53.node_type AS node_type_62 FROM show_off t0 LEFT JOIN users t23 ON t0.owner_id = t23.id LEFT JOIN aggregate_rating t34 ON t0.aggregate_rating_id = t34.id LEFT JOIN users t42 ON t0.author_id = t42.id LEFT JOIN users t53 ON t0.translator_id = t53.id ;

从mysql命令行运行时,它会返回所有数据。

有些地方的所有属性都被删除了。

我的实体看起来像是

<?php

namespace App\Entities;

use Doctrine\ORM\Mapping as ORM;
use App\Jobs\IndexNewEntitiesJob;
use App\Jobs\UpdateIndexEntitiesJob;
use Doctrine\Common\Collections\ArrayCollection;
use ApiArchitect\Compass\Entities\User;

/**
 * Class ShowOff
 *
 * @package Jkirkby91\DoctrineSchemas
 * @author James Kirkby <jkirkby91@gmail.com>
 *
 * @ORM\Entity
 * @ORM\HasLifeCycleCallbacks
 * @ORM\Table(name="show_off")
 * @ORM\Entity(repositoryClass="App\Repositories\ShowOffRepository")
 */
class ShowOff extends \App\Entities\MediaObject
{

    /**
     * @ORM\Column(type="string", length=45, nullable=false, unique=false)
     */
    protected $type;

    /**
     * @ORM\Column(type="integer", length=45, nullable=false)
     */
    protected $size;

    /**
     * @ORM\Column(type="datetime", length=45, nullable=false, unique=false)
     */
    protected $lastModified;

    /**
     * @ORM\OneToOne(targetEntity="\ApiArchitect\Compass\Entities\User", fetch="EAGER", cascade={"persist"})
     */
    protected $owner;

    /**
     * @ORM\OneToOne(targetEntity="\App\Entities\HairCutter", fetch="EAGER", cascade={"persist"})
     * @ORM\Column(nullable=true, unique=false)
     */
    protected $hairCutter;

    /**
     * @ORM\Column(type="string", length=255, nullable=false)
     */
    protected $filePath;


    /**
     * HairCutter constructor.
     * @param    $fileName   
     * @param    $filePath   
     * @param    $owner   
     * @param    $hairCutter   
     */
    public function __construct($fileName, $filePath, User $owner, $type, $lastModified, $size)
    {
        $this->name = $fileName;
        $this->filePath = $filePath;
        $this->owner = $owner;
        $this->type = $type;
        $this->lastModified = $lastModified;
        $this->size = $size;
        $this->nodeType = 'showOff';
    }

    /**
     * Gets the value of filePath.
     *
     * @return mixed
     */
    public function getFilePath()
    {
        return $this->filePath;
    }

    /**
     * Sets the value of filePath.
     *
     * @param mixed $filePath the file path
     *
     * @return self
     */
    protected function setFilePath($filePath)
    {
        $this->filePath = $filePath;

        return $this;
    }

    /**
     * Gets the value of owner.
     *
     * @return mixed
     */
    public function getOwner()
    {
        return $this->owner;
    }

    /**
     * Sets the value of owner.
     *
     * @param mixed $owner the owner
     *
     * @return self
     */
    protected function setOwner(User $owner)
    {
        $this->owner = $owner;

        return $this;
    }

    /**
     * Gets the value of hairCutter.
     *
     * @return mixed
     */
    public function getHairCutter()
    {
        return $this->hairCutter;
    }

    /**
     * Sets the value of hairCutter.
     *
     * @param mixed $hairCutter the hair cutter
     *
     * @return self
     */
    protected function setHairCutter($hairCutter)
    {
        $this->hairCutter = $hairCutter;

        return $this;
    }
}

我的配置看起来像是

<?php
return [

    'managers'                  => [
        'default' => [
            'dev'        => env('APP_DEBUG'),
            'meta'       => env('DOCTRINE_METADATA', 'annotations'),
            'connection' => env('DB_CONNECTION', 'sqlite'),
            'namespaces' => [
                'app'
            ],
            'paths'      => [
                env('COMPASS_ENTITIES',base_path('vendor/apiarchitect/compass/src/Entities')),
                env('AUTH_ENTITIES',base_path('vendor/apiarchitect/auth/src/Entities')),
                env('LOG_ENTITIES',base_path('vendor/apiarchitect/log/src/Entities')),
                env('NODE_ENTITIES',base_path('vendor/jkirkby91/lumendoctrinecomponent/src/Entities')),
                env('APP_ENTITIES',base_path('/app/Entities')),
            ],
            'repository' => Doctrine\ORM\EntityRepository::class,
            'proxies'    => [
                'namespace'     => false,
                'path'          => storage_path('proxies'),
                'auto_generate' => env('DOCTRINE_PROXY_AUTOGENERATE', false)
            ],

            'events'     => [
                'listeners'   => [],
                'subscribers' => []
            ],
            'filters'    => [],

            'mapping_types'              => [
                'enum' => 'string'
            ]
        ]
    ],

    'extensions'                => [
        LaravelDoctrine\Extensions\Timestamps\TimestampableExtension::class,
        LaravelDoctrine\Extensions\SoftDeletes\SoftDeleteableExtension::class,
        LaravelDoctrine\Extensions\Loggable\LoggableExtension::class,
        LaravelDoctrine\Extensions\Blameable\BlameableExtension::class,
        LaravelDoctrine\Extensions\IpTraceable\IpTraceableExtension::class,
        LaravelDoctrine\ORM\Extensions\TablePrefix\TablePrefixExtension::class,

    'custom_types'              => [
        'json' => LaravelDoctrine\ORM\Types\Json::class
    ],

    'custom_datetime_functions' => [],

    'custom_numeric_functions'  => [],

    'custom_string_functions'   => [],

    'logger'                    => env('DOCTRINE_LOGGER', trrue),

    'cache'                      => [
        'default'                => env('DOCTRINE_CACHE', 'memcached'),
        'namespace'              => null,
        'second_level'           => false,
    ],

    'gedmo'                     => [
        'all_mappings' => false
    ],

    'doctrine_presence_verifier' => true,
];

有人知道为什么会这样做吗?

1 个答案:

答案 0 :(得分:0)

你写下你的问题:

  

有些地方的所有属性都被删除了。

我怀疑showResponse方法中包含从存储库findAll()调用返回的结果集中的数据会发生一些事情。

尝试将结果与showResponse电话分开并检查您获得的结果:

public function index(ServerRequestInterface $request)
{
    $result = app()->make('em')->getRepository('App\Entities\ShowOff')->findAll();
    var_dump( $result ); // <-- should contain a collection of ShowOff entities.
    return $this->showResponse($result); // <-- something happens and returns an array
}