我在流明应用程序中使用 laravel 学说,在我的一个控制器中,我有一个索引函数,应该返回来自给定实体的所有记录。
函数如下所示
public function index(ServerRequestInterface $request)
{
return $this->showResponse(app()->make('em')->getRepository('AppEntitiesshowOff')->findAll());
}
返回这样的数据
[
{
"nodeType": 'showOff'
},
{
"nodeType": 'showOff'
},
{
"nodeType": 'showOff'
},
{
"nodeType": 'showOff'
}
]
这只是实体中的一个属性。
如果我打开原则调试器,则会看到执行的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 AppEntities;
use DoctrineORMMapping as ORM;
use AppJobsIndexNewEntitiesJob;
use AppJobsUpdateIndexEntitiesJob;
use DoctrineCommonCollectionsArrayCollection;
use ApiArchitectCompassEntitiesUser;
/**
* Class ShowOff
*
* @package Jkirkby91DoctrineSchemas
* @author James Kirkby <jkirkby91@gmail.com>
*
* @ORMEntity
* @ORMHasLifeCycleCallbacks
* @ORMTable(name="show_off")
* @ORMEntity(repositoryClass="AppRepositoriesShowOffRepository")
*/
class ShowOff extends AppEntitiesMediaObject
{
/**
* @ORMColumn(type="string", length=45, nullable=false, unique=false)
*/
protected $type;
/**
* @ORMColumn(type="integer", length=45, nullable=false)
*/
protected $size;
/**
* @ORMColumn(type="datetime", length=45, nullable=false, unique=false)
*/
protected $lastModified;
/**
* @ORMOneToOne(targetEntity="ApiArchitectCompassEntitiesUser", fetch="EAGER", cascade={"persist"})
*/
protected $owner;
/**
* @ORMOneToOne(targetEntity="AppEntitiesHairCutter", fetch="EAGER", cascade={"persist"})
* @ORMColumn(nullable=true, unique=false)
*/
protected $hairCutter;
/**
* @ORMColumn(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' => DoctrineORMEntityRepository::class,
'proxies' => [
'namespace' => false,
'path' => storage_path('proxies'),
'auto_generate' => env('DOCTRINE_PROXY_AUTOGENERATE', false)
],
'events' => [
'listeners' => [],
'subscribers' => []
],
'filters' => [],
'mapping_types' => [
'enum' => 'string'
]
]
],
'extensions' => [
LaravelDoctrineExtensionsTimestampsTimestampableExtension::class,
LaravelDoctrineExtensionsSoftDeletesSoftDeleteableExtension::class,
LaravelDoctrineExtensionsLoggableLoggableExtension::class,
LaravelDoctrineExtensionsBlameableBlameableExtension::class,
LaravelDoctrineExtensionsIpTraceableIpTraceableExtension::class,
LaravelDoctrineORMExtensionsTablePrefixTablePrefixExtension::class,
'custom_types' => [
'json' => LaravelDoctrineORMTypesJson::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,
];
有谁知道为什么要这样做?
你在问题中写道:
有些地方,我的所有属性都被剥离了。
我怀疑showResponse
方法中的数据发生了一些事情,您包装了从存储库返回的结果集,findAll()
调用。
尝试一次将结果与showResponse
调用分开,然后检查您得到的内容:
public function index(ServerRequestInterface $request)
{
$result = app()->make('em')->getRepository('AppEntitiesShowOff')->findAll();
var_dump( $result ); // <-- should contain a collection of ShowOff entities.
return $this->showResponse($result); // <-- something happens and returns an array
}