New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Product Component] no setter for id #1408
Comments
|
Hi @harikt If you need this |
|
@winzou if you are saying extend the class and add the But if you don't have a setter for Say |
|
I think there is something else that should be taken into account: encapsulation. It wouldn't be good oop to provide an interface that allows to arbitrary change the id of an object from the outside. The id setting should be delegated either as a private detail of impl, or passed in the ctor. |
|
@docteurklein may be I don't know, so would love to learn. Let us take a simple example of Post class. <?php
class Post
{
private $id;
private $title;
private $body;
public function __construct($id = null, $title = null, $body = null)
{
$this->id = $id;
$this->title = $title;
$this->body = $body;
}
public function getId()
{
return $this->id;
}
public function getTitle()
{
return $this->title;
}
public function getBody()
{
return $this->body;
}
public function setTitle($title)
{
$this->title = $title;
}
public function setBody($body)
{
$this->body = $body;
}
}Now consider you are fetching via Repository . $post_repository->find(1);You can set the id to the Now consider you are saving. When you save either you want to use Reflection to set the id value which you got from the mapper. Or do you find some other way without a setter like public function setId($id)
{
$this->id = $id;
} |
|
I think this can be closed.
If you really need some product identifier you should look at $variant = new Sylius\Component\Core\Model\ProductVariant();
$variant ->setSku('YOUR_RANDOM_SKU_DATA');
$product = new Sylius\Component\Product\Model\Product();
$product->setName('My awesome shirt');
$product->setMasterVariant($variant);
$service->save($product);
$variant = $repository->findOneBy(array('sku' => 'YOUR_RANDOM_SKU_DATA'));
$variant->getProduct(); |
|
@stloyd ok that moves to not using the same mysql id and making use of something like uuid. Thanks |
Hi guys,
Was working with the product component and noticed there is no setter for
id. How are you working on setting the id back when a Product is loaded or is saved.From the
product_serviceI would like to save the id back to theProductobject. Now the id is protected and there is no setter for it . In doctrine yes it works! , but for other db related we need a setter or it should be public.Thanks.
The text was updated successfully, but these errors were encountered: