我有一个类,它应该充当实体类(如 DDD 中)。它基本上看起来像这样:
public class Page
{
protected String Name;
protected String Description;
protected Dictionary<int, IField> Fields = new Dictionary<int, IField>();
protected Page SetName(String name)
{
this.Name = name;
return this;
}
protected Page SetDescription(String description)
{
this.Description = description;
return this;
}
public Page AddField(IField field)
{
this.Fields.add(xxx, Field); //xxx = just some id
return this;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我的问题是,这是一个有效的实体类吗?
我需要保持方法链接,所以请不要对此进行太多详细说明(即使您认为这是错误的)。
我主要关心的是,实体类是否可以包含方法,例如 getter 和 setter?尤其是像AddField?这样的方法
该AddField方法采用 类型的对象IField。我将其存储在班级内的字典中Page。那么这就是一个总和,对吗?
这不会改变实体的状态,使其不是真正的实体类吗?
还是就这样就可以了?
简单的问题,但我没有看到我的推理错误。有一个命名查询,其中选定的成员而不是整个行/实体作为结果。
现在我想使用方便的元组方法访问成员字段。
q = em.createNamedQuery("test.findvar");
List<Tuple> tuples = q.getResultList();
for (Tuple t : tuples)
System.out.println(t.get(0) + " " + t.get(1));
Run Code Online (Sandbox Code Playgroud)
不幸的是它让我困惑:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to javax.persistence.Tuple
Run Code Online (Sandbox Code Playgroud)
一切正常:
q = em.createNamedQuery("test.findvar");
List<Object[]> objs = q.getResultList();
for (Object[] obj : objs)
System.out.println(obj[0] + " " + obj[1]);
Run Code Online (Sandbox Code Playgroud)
我的第一个解决方案有什么问题?
在这篇文章中说:
实体框架使您可以将自定义数据类与数据模型一起使用,而无需对数据类本身进行任何修改.这意味着您可以将"普通旧"CLR对象(POCO)(例如现有域对象)与您的数据模型一起使用.这些POCO数据类(也称为持久性无知对象)映射到数据模型中定义的实体,支持大多数与实体生成的实体类型相同的查询,插入,更新和删除行为.数据模型工具.
POCO是一个有行为的DTO.
那么POCO和实体不一样吗?有什么区别?
我正在处理多语言帖子。我在 PostsTable 中添加了 beforefind() 以便我可以列出当前语言的帖子
public function beforeFind(Event $event, Query $query) {
$query->where(['Posts.locale' => I18n::locale()]);
}
Run Code Online (Sandbox Code Playgroud)
为了允许用户以不同语言复制帖子,我编写了以下功能:
public function duplicate(){
$this->autoRender = false;
$post_id= $this->request->data['post_id'];
$post = $this->Posts
->findById($post_id)
->select(['website_id', 'category_id', 'locale', 'title', 'slug', 'body', 'image', 'thumb', 'meta_title', 'meta_description', 'other_meta_tags', 'status'])
->first()
->toArray();
foreach($this->request->data['site'] as $site) {
if($site['name'] == false) {
continue;
}
$data = array_merge($post, [
'website_id' => $site['website_id'],
'locale' => $site['locale'],
'status' => 'Draft',
'duplicate' => true
]);
$pageData = $this->Posts->newEntity($data);
if($this->Posts->save($pageData)) {
$this->Flash->success(__('Post have been created.'));;
} …Run Code Online (Sandbox Code Playgroud) 有人能告诉我是否可以序列化有关系的实体?json_encode已经可以工作,但我的对象看起来像这样:
{
"id": 1,
"lot": 32,
"num": "533987",
"date_modification": {
"date": "2015-02-17 14:24:52",
"timezone_type": 3,
"timezone": "Europe/Paris"
},
"customer": {
"id": 1,
"lastname": "DUFRESNE",
"firstname": "CHRISTOPHE",
}
}
Run Code Online (Sandbox Code Playgroud)
但我想用withotu子对象序列化数据.事实上是这样的:
{
"id": 1,
"lot": 32,
"num": "533987",
"date_modification": "2015-02-17",
"customer": "DUFRESNE CHRISTOPHE",
}
}
Run Code Online (Sandbox Code Playgroud)
所以我检查了文档:http://symfony.com/doc/current/components/serializer.html 但我不知道是否可以使用像ManyToMany等关系,以及如何做到这一点?
这是我的实体:
/**
* Subscription (BS)
*
* @ORM\Table(name="subscription")
* @ORM\Entity(repositoryClass="Jcd\LiteyearBundle\Entity\SubscriptionRepository")
* @ORM\HasLifecycleCallbacks
*/
class Subscription {
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/** …Run Code Online (Sandbox Code Playgroud) 我必须在学说实体的注释中输入什么?
其实是这样的...
/**
* @ORM\Column(type="string", length=255)
*
* @Assert\Length(
* min=3,
* max=255,
* minMessage="The name is too short.",
* maxMessage="The name is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $name;
Run Code Online (Sandbox Code Playgroud)
我必须告诉教义类似的东西canBeNull=true。否则我总是会收到这个错误SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null
但是代码是什么?
好的,所以我正在创建我自己的实体组件系统,我坚持使用AddComponent Entity方法,它将组件添加到Enity中,它的外观如下:
template <typename T>
void AddComponent() {
NumOfComponents++;
AllComponents.push_back(new T());
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,但如果我有一个组件构造函数怎么办?像这样
class Transform : public Component
{
public:
Transfrm(Vector3f newPosition, Vector3f newRotation, Vector3f newScale) : Component("Transfrm") {};
Vector3f Position;
Vector3f Rotation;
Vector3f Scale;
~Transfrm();
};
Run Code Online (Sandbox Code Playgroud)
我想要实现的是这样的:
Entity ent1;
Vector3f Pos, Rot, Scl;
ent1.AddComponent<Transform>(Pos, Rot, Scl); // This is currently not possible
Run Code Online (Sandbox Code Playgroud)
如何接受Transform的方法参数作为AddComponent方法参数,并实现上述类似的功能?
我有一个错误:“不能使用 AppBundle\Entity 类型的对象作为数组”,这对我来说没有意义,我没关系。
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();
$repository = $this->getDoctrine()->getRepository(User::class);
$findUser = $repository->findOneBy(['username' => $form->getData()['username']]);
if ($findUser) {
return $this->redirectToRoute('login');
}
Run Code Online (Sandbox Code Playgroud)
错误无法使用 AppBundle\Entity\User 类型的对象,因为数组与“ $findUser = ”对齐
有人可以解释我的为什么它不起作用吗?
Symfony 3.3,PHP 7
如果在列表中找不到元素,我需要返回 null,但它返回一个空 guid。
mappedTypes.Where(x => x.ReferenceId == new Guid("1a087b71-638c-4f3c-b1cf-3b0438c181c0")).Select(x=>x.MappingId).FirstOrDefault()
Run Code Online (Sandbox Code Playgroud)
此查询仅返回 '00000000-0000-0000-0000-000000000000' - 我想返回 null - 或单个 guid 值(如果存在)。
每当我尝试使用 bin/console make:entity 创建实体时,都会得到以下结果:
php bin/console make:entity
Class name of the entity to create or update (e.g. FierceElephant):
> Video
In DebugClassLoader.php line 337:
Warning: include(/home/user/work/project1/vendor/composer/../../src/Entity/Video.php): failed to open stream: No such file or directory
Run Code Online (Sandbox Code Playgroud)
这个项目很新,目前代码很少;这是我尝试在项目中创建的第一个实体。我不明白为什么我会得到这个,因为很明显该文件不存在,因为我想创建它......我在谷歌上没有发现类似的问题。也许我忘了激活一个 php 扩展?
任何的想法?