Anonymous
Как отключить/удалить защищенное свойство
Сообщение
Anonymous » 17 ноя 2025, 20:06
У меня есть объект/класс Product следующим образом:
Код: Выделить всё
class Product
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Exclude()
* @ORM\Column(name="deletedAt", type="datetime", nullable=true)
*/
private $deletedAt;
/**
* @Assert\NotBlank()
* @Assert\MinLength( limit=3, message=" Product Name should have at least {{ limit }} characters.")
* @ORM\Column(name="name", type="string", length=100 , nullable=false)
*/
protected $name;
/**
* @var datetime $created
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
private $created;
/**
* @var datetime $updated
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
*/
private $updated;
/**
* @ORM\Column(name="description", type="string", length=350)
*/
protected $description;
/**
* @Assert\NotBlank()
* @ORM\Column(name="code", type="string", length=100)
*/
protected $code;
/**
* @Assert\NotBlank()
* @ORM\ManyToOne(targetEntity="Shopious\MainBundle\Entity\Category", inversedBy="products")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
*/
protected $category;
/**
* @Assert\NotBlank()
* @Assert\Min(limit = "0", message = "negative number is invalid")
* @Assert\Type(type="float", message="The value {{ value }} is not a valid number.")
* @ORM\Column(name="price", type="float")
*/
protected $price;
/**
* @Assert\NotBlank()
* @Assert\Min(limit = "0", message = "negative number is invalid")
* @Assert\Type(type="float", message="The value {{ value }} is not a valid number.")
* @ORM\Column(name="height", type="float")
*/
protected $height;
/**
* @Assert\NotBlank()
* @Assert\Min(limit = "0", message = "negative number is invalid")
* @Assert\Type(type="float", message="The value {{ value }} is not a valid number.")
* @ORM\Column(name="width", type="float")
*/
protected $width;
/**
* @Assert\NotBlank()
* @Assert\Min(limit = "0", message = "negative number is invalid")
* @Assert\Type(type="float", message="The value {{ value }} is not a valid number.")
* @ORM\Column(name="length", type="float")
*/
protected $length;
/**
* @Assert\NotBlank()
* @Assert\Min(limit = "0", message = "negative number is invalid")
* @Assert\Type(type="float", message="The value {{ value }} is not a valid number.")
* @ORM\Column(name="weight", type="float" )
*/
protected $weight;
/**
* @Accessor(getter="getShopRef")
* @ORM\ManyToOne(targetEntity="Shopious\MainBundle\Entity\Shop", inversedBy="products")
* @ORM\JoinColumn(name="shop_id", referencedColumnName="id", onDelete="CASCADE" , nullable=false)
*/
protected $shop;
/**
* @ORM\OneToMany(targetEntity="ProductAttribute", mappedBy="product", cascade={"persist","remove"})
*/
protected $attributes;
}
Итак, у меня есть объект «Продукт», и я хотел удалить все атрибуты этого продукта, что я и сделал:
однако он жаловался, что к атрибутам невозможно получить доступ. Как можно установить нулевые атрибуты Продукта?
Подробнее здесь:
https://stackoverflow.com/questions/170 ... d-property
1763399197
Anonymous
У меня есть объект/класс Product следующим образом: [code]class Product { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @Exclude() * @ORM\Column(name="deletedAt", type="datetime", nullable=true) */ private $deletedAt; /** * @Assert\NotBlank() * @Assert\MinLength( limit=3, message=" Product Name should have at least {{ limit }} characters.") * @ORM\Column(name="name", type="string", length=100 , nullable=false) */ protected $name; /** * @var datetime $created * @Gedmo\Timestampable(on="create") * @ORM\Column(type="datetime") */ private $created; /** * @var datetime $updated * @Gedmo\Timestampable(on="update") * @ORM\Column(type="datetime") */ private $updated; /** * @ORM\Column(name="description", type="string", length=350) */ protected $description; /** * @Assert\NotBlank() * @ORM\Column(name="code", type="string", length=100) */ protected $code; /** * @Assert\NotBlank() * @ORM\ManyToOne(targetEntity="Shopious\MainBundle\Entity\Category", inversedBy="products") * @ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="CASCADE", nullable=true) */ protected $category; /** * @Assert\NotBlank() * @Assert\Min(limit = "0", message = "negative number is invalid") * @Assert\Type(type="float", message="The value {{ value }} is not a valid number.") * @ORM\Column(name="price", type="float") */ protected $price; /** * @Assert\NotBlank() * @Assert\Min(limit = "0", message = "negative number is invalid") * @Assert\Type(type="float", message="The value {{ value }} is not a valid number.") * @ORM\Column(name="height", type="float") */ protected $height; /** * @Assert\NotBlank() * @Assert\Min(limit = "0", message = "negative number is invalid") * @Assert\Type(type="float", message="The value {{ value }} is not a valid number.") * @ORM\Column(name="width", type="float") */ protected $width; /** * @Assert\NotBlank() * @Assert\Min(limit = "0", message = "negative number is invalid") * @Assert\Type(type="float", message="The value {{ value }} is not a valid number.") * @ORM\Column(name="length", type="float") */ protected $length; /** * @Assert\NotBlank() * @Assert\Min(limit = "0", message = "negative number is invalid") * @Assert\Type(type="float", message="The value {{ value }} is not a valid number.") * @ORM\Column(name="weight", type="float" ) */ protected $weight; /** * @Accessor(getter="getShopRef") * @ORM\ManyToOne(targetEntity="Shopious\MainBundle\Entity\Shop", inversedBy="products") * @ORM\JoinColumn(name="shop_id", referencedColumnName="id", onDelete="CASCADE" , nullable=false) */ protected $shop; /** * @ORM\OneToMany(targetEntity="ProductAttribute", mappedBy="product", cascade={"persist","remove"}) */ protected $attributes; } [/code] Итак, у меня есть объект «Продукт», и я хотел удалить все атрибуты этого продукта, что я и сделал: [code]unset(product->attributes) [/code] однако он жаловался, что к атрибутам невозможно получить доступ. Как можно установить нулевые атрибуты Продукта? Подробнее здесь: [url]https://stackoverflow.com/questions/17016728/how-to-unset-remove-a-protected-property[/url]