0

I’m a total noob at Symphony but I’m trying to learn. The documentation has not been able to help me with this specific problem I’m having.

How can I do something like this:

// if ($products are in userCart) {
//     show those only
// }

I’m struggling with finding a way to get that information. I’ve tried many attempts.

I am successfully flushing my products to the database and my associations are as follows:

Association Mappings

I want to do this in showCartAction function:

$user = $this->getUser();
$em = $this->getDoctrine()->getManager();

//then get the specific products

$products = $em->getRepository(‘ShopBundle:??’)->??

Please any help is appreciated, thanks for your time.

1 Answer 1

1

Assuming the entity is named Product:

// Your model, you can use it to fetch products
$productRepository = $em->getRepository('ShopBundle:Product');

// Retrieve all products as an array of objects
$products = $productRepository->findAll();

// Retrieve a specific product as object from its reference (id)
$product = $productRepository->find(1); // Returns product with 'id' = 1

// Retrieve a specific product based on condition(s)
$product = $productRepository->findOneBy(['price' => 10]);

// Retrieve many products based on condition(s)
$products = $productRepository->findBy(['price' => 10]);

To check if a specific product is in your UserCart object:

$cartRepository = $em->getRepository('ShopBundle:UserCart');

// Fetches the cart by its owner (the current authenticated user)
// assuming UserCart has a $user property that represents an association to your User entity
$cart = $cartRepository->findOneBy(['user' => $this->getUser()]);

// Check if a specific product is in the cart,
// assuming $product is a Product object retrieved like shown above
if ($cart->contains($product)) {
    // Do something
}

For the whole reference, see Working with objects from Doctrine documentation.

I hope that will help you.
Do not hesitate to post a comment if you need more precisions or any other information.

EDIT

To access properties of an object, use its getters:

$cartView = array(
    'products' => $cart->getProducts(), // return the property $products
    'user'     => $cart->getUser(),     // return the property $user
);

That is possible only if the methods exist and have public access.

Note You should really look more at OOP and practice it before use a framework like Symfony.

Sign up to request clarification or add additional context in comments.

6 Comments

This is very good but $cart when I print is still an object. I want it to print what's inside of the cart. I hope I am being understood. I will make an edit to show
Look at my edit. I show you how access the properties of your object.
Thank You very much, I will work and touch back later
You're welcome. Don't forget to accept the answer that solves your problem, if there is one, it's the better thanks that you can give, and will surely help people with the same issue.
I'm almost there I think but how can I instead do $cartRepo = $em->getRepository('PascalShopTestBundle:Quantity'); $cart = $cartRepo->findOneBy(['userCart' => $cartRepo->getUserCart()]);?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.