gpt4 book ai didi

php - Laravel Doctrine 仅从 findAll() 查询返回单个属性

转载 作者:行者123 更新时间:2023-11-29 03:22:23 26 4
gpt4 key购买 nike

我在 lumen 应用程序中使用 laravel Doctrine ,在我的一个 Controller 中我有一个索引函数,它应该返回给定实体的所有记录。

函数看起来像这样

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,
];

有谁知道为什么会这样吗?

最佳答案

你在问题​​中写下:

Some where along the line all my attributes are being stripped out.

我怀疑 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
}

关于php - Laravel Doctrine 仅从 findAll() 查询返回单个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42019091/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com