Skip to content
Snippets Groups Projects
Commit 32e12a75 authored by damjan's avatar damjan
Browse files

[TASK] Property repository and form are adapted

Also, category is deleted from property.
TODO: Adapt property controller
parent d464ff93
No related branches found
No related tags found
No related merge requests found
<?php
namespace SGalinski\TypoScriptBackendBundle\Entity;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
/**
* AttributeRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class AttributeRepository extends EntityRepository {
/**
* Persists attribute to database if it doesn't exist in database.
* If the attribute is already persisted (has id), it is updated (merged).
*
* @param Attribute $attribute
* @return Attribute
*/
public function persistAttribute(Attribute $attribute) {
if ($attribute->getId() !== NULL) {
$attribute = $this->_em->merge($attribute);
} else {
$this->_em->persist($attribute);
}
$this->_em->flush();
return $attribute;
}
/**
* Deletes Attribute.
*
* @param Attribute $attribute
*/
public function deleteAttribute(Attribute $attribute) {
$this->_em->remove($attribute);
$this->_em->flush();
}
/**
* Finds attributes by parameters (if set): $isType, $typo3Group, $typo3Version.
* If $typo3Version is set, filters attributes with $typo3Version in minVersion-maxVersion range.
* Returns persisted objects.
*
* @param boolean $isType
* @param integer $typo3Group
* @param string $typo3Version - if NULL, fetches all versions.
* @return array of persisted objects
*/
public function findByVersionInRange($isType = NULL, $typo3Group = Attribute::NORMAL_GROUP, $typo3Version = NULL) {
$query = $this->selectQueryByIsTypeGroupVersion($isType, $typo3Group, $typo3Version);
$attributes = $query->getResult();
return $attributes;
}
/**
* Fetches by id a record from attribute table.
* Doesn't fetch records referenced by foreign keys, but fetches those foreign keys with other record data.
* Example of returned value:
*
* {
* "id": 55,
* "name": "kr",
* "minVersion": "1.0",
* "maxVersion": "7.4",
* "typo3Group": 1,
* "isType": false,
* "deleted": false,
* "type_id": "14",
* "parent_id": "59"
* }
*
* @param $id
* @return array - associative array
*/
public function findByIdForeignKeysInsteadOfObjects($id) {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->createQueryBuilder('attribute')
->where('attribute.id = :id')
->setParameter('id', $id);
/** @var Query $query */
$query = $queryBuilder->getQuery();
$query->setHint(Query::HINT_INCLUDE_META_COLUMNS, TRUE);
$attributeArray = $query->getArrayResult();
return $attributeArray[0];
}
/**
* Fetches by name a type from attribute table.
* Doesn't fetch records referenced by foreign keys, but fetches those foreign keys with other record data.
* Example of returned value:
*
* {
* "id": 55,
* "name": "XXX",
* "minVersion": "1.0",
* "maxVersion": "7.4",
* "typo3Group": 1,
* "isType": true,
* "deleted": false,
* "parent_id": "59"
* }
*
* @param $urlName
* @return array - associative array
*/
public function findTypeByUrlNameForeignKeysInsteadOfObjects($urlName) {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->createQueryBuilder('attribute')
->where('attribute.urlName = :urlName')
->andWhere('attribute.isType = TRUE')
->andWhere('attribute.deleted = 0')
->setParameter('urlName', $urlName);
/** @var Query $query */
$query = $queryBuilder->getQuery();
$query->setHint(Query::HINT_INCLUDE_META_COLUMNS, TRUE);
$attributeArray = $query->getArrayResult();
return $attributeArray[0];
}
/**
* Returns children with parent_id = $id as en array.
*
* @param $id - parent_id
* @param string|NULL $orderBy
* @return array
*/
public function getChildrenByParentId($id, $orderBy = NULL) {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->createQueryBuilder('attribute')
->where('attribute.parent = :id')
->andWhere('attribute.deleted IS NULL OR attribute.deleted = FALSE')
->setParameter('id', $id);
if ($orderBy) {
$queryBuilder->orderBy('attribute.' . $orderBy);
}
/** @var Query $query */
$query = $queryBuilder->getQuery();
$query->setHint(Query::HINT_INCLUDE_META_COLUMNS, TRUE);
$attributeArray = $query->getArrayResult();
return $attributeArray;
}
/**
* Returns properties with parent_id = $id as en array with names of property types.
* Returns only properties which are not marked as deleted.
*
* @param $id - parent_id
* @param string|NULL $orderBy
* @param int $typo3Group
* @param string $typo3Version
* @return array example: [{"property":{"id":1163,"name":"wrap","description":"Wraps the links.","minVersion":"1.0","maxVersion":"7.4",
* example: [{"property":{"id":1163,"name":"wrap","description":"Wraps the links.","minVersion":"1.0","maxVersion":"7.4",
* "typo3Group":1,"default":"\n","isType":false,"deleted":false,"type_id":"687","parent_id":"1164"},
* "type_name":"wrap","type_deleted":false}, ...]
*/
public function getPropertiesByParentIdWithTypeNames(
$id, $orderBy = NULL, $typo3Group = Attribute::NORMAL_GROUP, $typo3Version = NULL
) {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->createQueryBuilder('attribute')
->select(
'attribute AS property, property_type.name AS type_name, property_type.urlName AS type_url_name, property_type.deleted AS type_deleted'
)
->from('TypoScriptBackendBundle:Attribute', 'property_type')
->where('attribute.parent = :id AND property_type.id = attribute.type')
->andWhere(':typo3Group IS NULL OR attribute.typo3Group = :typo3Group')
->andWhere(':typo3Version IS NULL OR attribute.minVersion IS NULL OR attribute.minVersion <= :typo3Version')
->andWhere(':typo3Version IS NULL OR attribute.maxVersion IS NULL OR attribute.maxVersion >= :typo3Version')
->andWhere('attribute.deleted IS NULL OR attribute.deleted = FALSE')
->setParameter('typo3Group', $typo3Group)
->setParameter('typo3Version', $typo3Version)
->setParameter('id', $id);
if ($orderBy) {
$queryBuilder->orderBy('attribute.' . $orderBy);
}
/** @var Query $query */
$query = $queryBuilder->getQuery();
$query->setHint(Query::HINT_INCLUDE_META_COLUMNS, TRUE);
$attributeArray = $query->getArrayResult();
return $attributeArray;
}
/**
* Lists all attributes by $version, $isType, $typo3Group parameters.
* Returns non persisted array of attributes which are not marked as deleted.
*
* @param $version
* @param $isType
* @param $typo3Group
* @param bool $namesOnly - If true returns names, urlNames and id only
* @param string|NULL $orderBy
* @return array - non persisted array of attributes
*/
public function listAttributes($version, $isType, $typo3Group, $namesOnly = FALSE, $orderBy = NULL) {
// TODO: "namesOnly" optimisation is dropped because the query "namesOnlySelectQueryByIsTypeGroupVersion"
// selects only attributes which have NOT NULL in category field.
// if ($namesOnly) {
// $query = $this->namesOnlySelectQueryByIsTypeGroupVersion($isType, $typo3Group, $version, $orderBy);
// } else {
$query = $this->selectQueryByIsTypeGroupVersion($isType, $typo3Group, $version, $orderBy);
// }
$query->setHint(Query::HINT_INCLUDE_META_COLUMNS, TRUE);
$attributeArray = $query->getArrayResult();
return $attributeArray;
}
/**
* Returns the query which lists all attributes by $version, $isType, $typo3Group parameters.
* The query returns only attributes which are not marked as deleted.
* All columns are selected.
*
* @param $isType
* @param $typo3Group
* @param $typo3Version
* @param string|NULL $orderBy
* @return Query
*/
protected function selectQueryByIsTypeGroupVersion($isType, $typo3Group, $typo3Version, $orderBy = NULL) {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->createQueryBuilder('attribute')
->select('attribute')
->leftJoin('attribute.category', 'categoryTable')
->where(':isType IS NULL OR attribute.isType = :isType')
->andWhere(':typo3Group IS NULL OR attribute.typo3Group = :typo3Group')
->andWhere(':typo3Version IS NULL OR attribute.minVersion IS NULL OR attribute.minVersion <= :typo3Version')
->andWhere(':typo3Version IS NULL OR attribute.maxVersion IS NULL OR attribute.maxVersion >= :typo3Version')
->andWhere('attribute.deleted IS NULL OR attribute.deleted = FALSE')
->setParameter('isType', $isType)
->setParameter('typo3Group', $typo3Group)
->setParameter('typo3Version', $typo3Version);
if ($orderBy) {
$queryBuilder->addOrderBy('categoryTable.name', 'DESC')
->addOrderBy('attribute.' . $orderBy);
}
/** @var Query $query */
$query = $queryBuilder->getQuery();
return $query;
}
/**
* Returns the query which lists all attributes by $version, $isType, $typo3Group parameters.
* The query returns only attributes which are not marked as deleted.
* Only id, urlName and name columns are selected.
*
* @param $isType
* @param $typo3Group
* @param $typo3Version
* @param string|NULL $orderBy
* @return Query
*/
protected function namesOnlySelectQueryByIsTypeGroupVersion($isType, $typo3Group, $typo3Version, $orderBy = NULL) {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->createQueryBuilder('attribute')
->select('attribute.id, attribute.urlName, attribute.name AS name, categoryTable.name AS category')
->from('TypoScriptBackendBundle:Category', 'categoryTable')
->where('attribute.category = categoryTable.id')
->andWhere(':isType IS NULL OR attribute.isType = :isType')
->andWhere(':typo3Group IS NULL OR attribute.typo3Group = :typo3Group')
->andWhere(':typo3Version IS NULL OR attribute.minVersion IS NULL OR attribute.minVersion <= :typo3Version')
->andWhere(':typo3Version IS NULL OR attribute.maxVersion IS NULL OR attribute.maxVersion >= :typo3Version')
->andWhere('attribute.deleted IS NULL OR attribute.deleted = FALSE')
->setParameter('isType', $isType)
->setParameter('typo3Group', $typo3Group)
->setParameter('typo3Version', $typo3Version);
if ($orderBy) {
$queryBuilder->addOrderBy('categoryTable.name', 'DESC')
->addOrderBy('attribute.' . $orderBy);
}
/** @var Query $query */
$query = $queryBuilder->getQuery();
return $query;
}
}
\ No newline at end of file
......@@ -101,14 +101,6 @@ class Property {
*/
private $parentProperty;
/**
* @var Category
*
* @ORM\ManyToOne(targetEntity="Category")
* @ORM\JoinColumn(name="category", referencedColumnName="id")
*/
private $category;
/**
* @var ArrayCollection
*
......@@ -346,23 +338,6 @@ class Property {
return $this;
}
/**
* @return Category
*/
public function getCategory() {
return $this->category;
}
/**
* @param Category $category
* @return Property
*/
public function setCategory($category) {
$this->category = $category;
return $this;
}
/**
* @return Property
*/
......
<?php
namespace SGalinski\TypoScriptBackendBundle\Entity;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
/**
* PropertyRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class PropertyRepository extends EntityRepository {
/**
* Persists property to database if it doesn't exist in database.
* If the property is already persisted (has id), it is updated (merged).
*
* @param Property $property
* @return Property
*/
public function persistProperty(Property $property) {
if ($property->getId() !== NULL) {
$property = $this->_em->merge($property);
} else {
$this->_em->persist($property);
}
$this->_em->flush();
return $property;
}
/**
* Deletes Property.
*
* @param Property $property
*/
public function deleteProperty(Property $property) {
$this->_em->remove($property);
$this->_em->flush();
}
/**
* Finds properties by parameters (if set): $typo3Group, $typo3Version.
* If $typo3Version is set, filters properties with $typo3Version in minVersion-maxVersion range.
* Returns persisted objects.
*
* @param integer $typo3Group
* @param string $typo3Version - if NULL, fetches all versions.
* @return array of persisted objects
*/
public function findByVersionInRange($typo3Group = Type::NORMAL_GROUP, $typo3Version = NULL) {
$query = $this->selectQueryByGroupAndVersion($typo3Group, $typo3Version);
$properties = $query->getResult();
return $properties;
}
/**
* Fetches by id a record from property table.
* Doesn't fetch records referenced by foreign keys, but fetches those foreign keys, also fetches other record data.
* Example of returned value:
*
* {
* "id": 55,
* "name": "kr",
* "minVersion": "1.0",
* "maxVersion": "7.4",
* "typo3Group": 1,
* "deleted": false,
* "type_id": "14",
* "parent_type_id": "59"
* }
*
* @param $id
* @return array - associative array
*/
public function findByIdForeignKeysInsteadOfObjects($id) {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->createQueryBuilder('property')
->where('property.id = :id')
->setParameter('id', $id);
/** @var Query $query */
$query = $queryBuilder->getQuery();
$query->setHint(Query::HINT_INCLUDE_META_COLUMNS, TRUE);
$propertyArray = $query->getArrayResult();
return $propertyArray[0];
}
/**
* Returns properties with parent_type_id = $id as en array (non persisted).
*
* @param $id - parent_id
* @param string|NULL $orderBy
* @return array
*/
public function getPropertiesByParentTypeId($id, $orderBy = NULL) {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->createQueryBuilder('property')
->where('property.parentType = :id')
->andWhere('property.deleted IS NULL OR property.deleted = FALSE')
->setParameter('id', $id);
if ($orderBy) {
$queryBuilder->orderBy('property.' . $orderBy);
}
/** @var Query $query */
$query = $queryBuilder->getQuery();
$query->setHint(Query::HINT_INCLUDE_META_COLUMNS, TRUE);
$propertyArray = $query->getArrayResult();
return $propertyArray;
}
/**
* Returns properties with parent_type_id = $id as en array with names of property types.
* Returns only properties which are not marked as deleted.
*
* @param $id - parent_type_id
* @param string|NULL $orderBy
* @param int $typo3Group
* @param string $typo3Version
* @return array example: [{"property":{"id":1163,"name":"wrap","description":"Wraps the links.","minVersion":"1.0","maxVersion":"7.4",
* example: [{"property":{"id":1163,"name":"wrap","description":"Wraps the links.","minVersion":"1.0","maxVersion":"7.4",
* "typo3Group":1,"default":"\n","deleted":false,"type_id":"687","parent_id":"1164"},
* "type_name":"wrap","type_deleted":false}, ...]
*/
public function getPropertiesByParentTypeIdWithTypeNames(
$id, $orderBy = NULL, $typo3Group = Type::NORMAL_GROUP, $typo3Version = NULL
) {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->createQueryBuilder('property')
->select(
'property AS property, property_type.name AS type_name, property_type.urlName AS type_url_name, property_type.deleted AS type_deleted'
)
->from('TypoScriptBackendBundle:Property', 'property_type')
->where('property.parent = :id AND property_type.id = property.type')
->andWhere(':typo3Group IS NULL OR property.typo3Group = :typo3Group')
->andWhere(':typo3Version IS NULL OR property.minVersion IS NULL OR property.minVersion <= :typo3Version')
->andWhere(':typo3Version IS NULL OR property.maxVersion IS NULL OR property.maxVersion >= :typo3Version')
->andWhere('property.deleted IS NULL OR property.deleted = FALSE')
->setParameter('typo3Group', $typo3Group)
->setParameter('typo3Version', $typo3Version)
->setParameter('id', $id);
if ($orderBy) {
$queryBuilder->orderBy('property.' . $orderBy);
}
/** @var Query $query */
$query = $queryBuilder->getQuery();
$query->setHint(Query::HINT_INCLUDE_META_COLUMNS, TRUE);
$propertyArray = $query->getArrayResult();
return $propertyArray;
}
/**
* Lists all properties by $version, $isType, $typo3Group parameters.
* Returns non persisted array of properties which are not marked as deleted.
*
* @param $version
* @param $isType
* @param $typo3Group
* @param string|NULL $orderBy
* @return array - non persisted array of properties
*/
public function listProperties($version, $isType, $typo3Group, $orderBy = NULL) {
$query = $this->selectQueryByGroupAndVersion($isType, $typo3Group, $version, $orderBy);
$query->setHint(Query::HINT_INCLUDE_META_COLUMNS, TRUE);
$propertyArray = $query->getArrayResult();
return $propertyArray;
}
/**
* Returns the query which lists all properties by $version, $typo3Group parameters.
* The query returns only properties which are not marked as deleted.
* All columns are selected.
*
* @param $isType
* @param $typo3Group
* @param $typo3Version
* @param string|NULL $orderBy
* @return Query
*/
protected function selectQueryByGroupAndVersion($typo3Group, $typo3Version, $orderBy = NULL) {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->createQueryBuilder('property')
->select('property')
->leftJoin('property.category', 'categoryTable')
->where(':isType IS NULL OR property.isType = :isType')
->andWhere(':typo3Group IS NULL OR property.typo3Group = :typo3Group')
->andWhere(':typo3Version IS NULL OR property.minVersion IS NULL OR property.minVersion <= :typo3Version')
->andWhere(':typo3Version IS NULL OR property.maxVersion IS NULL OR property.maxVersion >= :typo3Version')
->andWhere('property.deleted IS NULL OR property.deleted = FALSE')
->setParameter('isType', $isType)
->setParameter('typo3Group', $typo3Group)
->setParameter('typo3Version', $typo3Version);
if ($orderBy) {
$queryBuilder->addOrderBy('categoryTable.name', 'DESC')
->addOrderBy('property.' . $orderBy);
}
/** @var Query $query */
$query = $queryBuilder->getQuery();
return $query;
}
}
\ No newline at end of file
......@@ -6,7 +6,7 @@ use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class AttributeType extends AbstractType {
class PropertyType extends AbstractType {
/**
* @param FormBuilderInterface $builder
* @param array $options
......@@ -21,10 +21,9 @@ class AttributeType extends AbstractType {
->add('minVersion')
->add('maxVersion')
->add('typo3Group')
->add('isType')
->add('deleted')
->add('parent')
->add('category');
->add('parentType')
->add('parentProperty');
}
/**
......@@ -33,7 +32,7 @@ class AttributeType extends AbstractType {
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(
[
'data_class' => 'SGalinski\TypoScriptBackendBundle\Entity\Attribute',
'data_class' => 'SGalinski\TypoScriptBackendBundle\Entity\Property',
'csrf_protection' => false,
]
);
......@@ -43,6 +42,6 @@ class AttributeType extends AbstractType {
* @return string
*/
public function getName() {
return 'sgalinski_typoscriptbackendbundle_attribute';
return 'sgalinski_typoscriptbackendbundle_property';
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment