How can I retrieve, say, all the lineitems for all orders in a customer object?
I am trying
grdView.DataSource = customer.Orders.
but after orders, all I get is "GetMulti"...I don't see the lineitems collection.
I can understand doing this for one order
grdView.DataSource = customer.Orders(0).LineItems
but how do I get all the lineitems for all orders?
I created the customer object
I added order 1
I added items to the order 1
I created order 2
I added items to order 2
my entities are Customer, Order, LineItem
I want to display all the lineitems in a gridview before saving. How can I do this using llblgen pro runtime?
In order to get all of the LineItem entities, you should use relations and filters to populate a LineItemCollection. Here is the LLBLGen Documentation for multi-entity filters. To be able to filter the results on the specific customer you want, you need to add relations to get the related entities required.
(This assumes you are using SelfServicing. Check documentation for Adapter.)
// Make a link through relations from LineItem to Order to Customer
RelationCollection relations = new RelationCollection();
relations.Add(LineItemEntity.Relations.OrderEntityUsingLineItemId);
relations.Add(OrderEntity.Relations.CustomerEntityUsingOrderId);
// Filter on the customer id
PredicateExpression filter = new PredicateExpression();
filter.Add(CustomerFields.CustomerId == CustomerId);
// Get LineItems based on the relations and filters above
LineItemCollection collection = new LineItemCollection();
collection.GetMulti(filter, 0, null, relations);
Related
Is there a way to get the record or records that was inserted by sync method?
Something like:
$shop_product = $shop->products()->sync(1);
so I want now $shop_product to be instance that contains the records
{
shop_id: 1,
product_id: 1
}
The sync method updates your products relationship with shop.
So calling $shop->products should return you only products that you synced.
There is no reason to access pivot table data unless you have some additional data there. If you have then you should define in your model relationship that you want it. Like:
return $this->manyToMany(.....)->withPivot(['data_field_name'])
If you are working with shop products you probably don't need pivot table. Usually one product can be related to only one shop so relation will be $this->hasMany(Product::class) and your products table will have shop_id
I have an entity called Path, which is linked to another entity Product using two different fields.
/**
* #ORM\Entity
*/
class Path
{
/**
* #ORM\ManyToOne(targetEntity="Product")
*/
private $product;
/**
* #ORM\ManyToMany(targetEntity="Product")
*/
private $products;
}
Links in different fields are done due to the need to perform some logic on "main" product, while allowing attaching to multiple products in some other parts of the app.
EDIT: Product in product field is not present in products field.
I'm looking to build a query that will do some other checks against Product, possibly joining with other entities related to Product. I'd like database to perform only one join with product table (as I would if it weren't done in Doctrine).
Currently I've got a QueryBuilder set up like so:
$qb->leftJoin("path.products", 'path_product');
$qb->join(Product::class, 'products', 'WITH', "path.product = products OR path_product.id = products.id")
Generated SQL:
SELECT path.id AS id_0
FROM path path
LEFT JOIN path_product path_product ON path.id = path_product.path_id
LEFT JOIN product product_1 ON product_1.id = path_product.product_id
LEFT JOIN product product_2 ON (path.product_id = product_2.id OR product_1.id = product_2.id)
(It's a simplified SQL generated from Doctrine Query Builder with unnecessary fields removed and expanded aliases)
...but this generates one additional join with Product's table, which feels "dirty". Is there some other way to do a join like this?
I have the following Entity Framework model which i am retrieving a number of marketing_campaign entities. A Marketing campaign can have multiple groups and each group can have multiple stores.
What i need to be able to do is select all marketing campaigns for a particular StoreId. I know how to do the query for a single nested entity e.g. Groups.SelectMany(n => n.StoresInGroups).Where(s=>s.StoreId == 2); but not sure how to nest it deep enough to get the desired result.
Edit: Clearer picture
var context = new context(); // init your context here
var query =
from sig in context.Store.Single(p=>p.StoreId = 2).StoresInGroup //filter out by particular toreId
from grp in sig.Group.Marketing_Groups
from mc in grp.MarketingCampaign
select mc;
I'm just copy & pasting some of the introductory text from one of my questions, since the same table relationship is involved in this question also.
I have three of many tables in Oracle (10g) database as listed below. I'm using Hibernate Tools 3.2.1.GA with Spring version 3.0.2.
Product - parent table
Colour - parent table
ProductColour - join table - references colourId and prodId of Colour and Product tables respectively
Where the table ProductColour is a join table between Product and Colour. As the table names imply, there is a many-to-many relationship between Product and Colour which is mapped by PrductColour. I think, the relationship in the database can easily be imagined and is clear with only this much information. Therefore, I'm not going to explore this relationship at length unnecessarily.
An entity (row) in Product is associated with any number entities in Colour and an entity (row) in Colour can also be associated with any number of entities in Product.
Since, it is a many-to-many relationship, it is mapped in the Product and the Colour entity classes (POJOs) with their respective java.util.Set and no direct POJO class for the product_colour table is available.
The class Product looks like the following.
public class Product implements java.io.Serializable
{
private BigDecimal prodId;
private Set<Colour> colours = new HashSet<Colour>(0);
.
.
.
//Other properties with setters and getters.
}
The class Colour looks like the following.
public class Colour implements java.io.Serializable
{
private BigDecimal colourId;
private Set<Product> products = new HashSet<Product>(0);
.
.
.
//Other properties with setters and getters.
}
The actual mapping between entities is available in xxx.hbm.xml files, regarding this question which is unnecessary, I think .
What I want to do is to retrieve only those rows from the Colour table which don't match the colour rows in the ProductColour table for a particular product at a time. In this regard, the native Oracle SQL statement would look something like the following.
SELECT colour_id, colour_name, colour_hex
FROM colour
WHERE colour_id not in (SELECT colour_id FROM product_colour WHERE prod_id=81)
ORDER BY colour_id DESC
Where prod_id can be any valid BigDecimal number in Java which is dynamic.
As noted earlier, the relationship is available as a many-to-many relationship in Hibernate, no POJO class for the database table product_colour is available and therefore, I'm fumbling in writing such an HQL statement in Hibernate. I have tried to write such an HQL statement but no attempts were succeeded.
[The code presented in the rest of the part may completely be unnecessary to review]
I'm therefore following a traditional way. What I'm doing is... I'm first retrieving a single product row from the Product the entity class based on a dynamic value of prodId such as,
List<Product>list=session.createQuery("from Product where prodId=:prodId")
.setParameter("prodId", prodId).list();
and then using a loop, I'm getting the entire Colour set - java.util.Set corresponding to the product_colour table in Oracle which is available in the Product entity for this product such as,
Set<Colour>colours=new HashSet<Colour>(0);
for(Product p:list)
{
if(p!=null)
{
colours=p.getColours();
}
}
As can be seen, the colours Set is being populated with all of the colour rows available (reference rows) in the product_colour table in Oracle.
After getting all of these rows, I'm getting the entire Colour entity class itself (all the row in it) that corresponds to the colour table in Oracle and then removing those rows which match the rows retrieved from the product_colour Oracle table (available in the colours Set in the preceding snippet) satisfying the condition as mentioned earlier such as,
List<Colour>colourList=session.createQuery("from Colour order by colourId desc").list();
Iterator<Colour>it=colourList.iterator();
while(it.hasNext())
{
Colour c=(Colour)it.next();
for(Colour pc:colours) //colours is available in the preceding snippet.
{
if(c==pc)
{
it.remove();
}
}
}
This can do what is intended but doing so, may imply some overhead on the system. Additionally, what I want to achieve doesn't seem possible with this approach which is pagination. I can't use the setFirstResult(int) and the setMaxResults(int) methods to accomplish the task of pagination which is the case otherwise like the one shown below regarding the Product entity class,
List<Product> products=session.createQuery("from product order by prodId desc")
.setMaxResults(0).setFirstResult(4);
So the question is again, regarding this relationship, is this possible to write such an HQL statement that can retrieve only those rows from the Colour entity class which don't match the colour rows in the product_colour Oracle table like the native SQL statement shown above?
How can I achieve the concept of pagination otherwise (in case, it is not possible)?
Short answer to a veeeeery long question:
select colour from Colour colour
where colour.id not in (
select colour2.id from Product product
inner join product.colours colour2
where product.id = :productId)
Here is the expression
x => x.stf_Category.CategoryID == categoryId
x refers to an Product Entity that contains a Category. I am trying to load all Products that match given categoryId.
In the db the Product table contains a Foreign Key reference to Category (via CategoryId).
Question: I think I am doing it wrong. Is there something else one has to do in EF4 to create a LINQ expression of this type?
Are there any good examples of EF4 Linq expressions out there? Specifically something that queries on the basis of related entities such as my problem ?
Thanks !
You're looking for the Include method.
var query = db.Products.Include("Categories");
This is commonly referred to as eager loading.
Entity Framework will 'infer' the JOIN constraint based on the mapping you have specified.
The "magic string" needs to match the Entity Set name on your EDMX.
Check out this post for more info.
EDIT
I'm a little confused as to whether you want the Products and Categories, or just the Products which have a specific Category ID.
If the latter, this is the way to go:
var query = from p in db.products
join c in db.categories
on p.CategoryId equals c.CategoryId
where c.CategoryId == someCategoryId
select p;
Keep in mind though, the above query is exactly the same result as your original query.
If p is a product, then p.Categories will look at the Navigational Property of your Product entity on the EDMX, in which case it will be your Category FK.
As long as you setup your Navigational properties right, p.Categories is fine.
If you are using EF4 and the association between Category and Product classes has been picked up and defined in your Model, then all products with a specific categoryID can be selected as simple as:
x => x.CategoryID == categoryID
You don't need to join nor an eager loading for that.