extbase query builder injection of one repository to another repository - typo3

I have a question regarding extbase if I need to inject a reposotory into another reposotry for getting join condition in the query what I need to do for that?
For example this query
$query = $this->createQuery()
->statement('
SELECT tx_casmarketing_domain_model_category.uid, tx_casmarketing_domain_model_category.name
FROM tx_casmarketing_domain_model_category, tx_casmarketing_domain_model_ad
WHERE tx_casmarketing_domain_model_ad.ad_cat = tx_casmarketing_domain_model_category.uid ')
This query I am trying to access through
$query = $this->createQuery();
$query->matching(
);
Basically I am trying to inject one ad repository to category repository because there is no relationship defined in model for that.
my model contain category to ads relationship so when i access ads throughs adRepository I automcally accesss the cateogrys along with adCategory Repository, but in vise-virsa when i need category which has been selected for the ads i can only access through joining in query becus i do not have that relationship in my model. i need that category for my doropdown list in ads that how many ads category corrently ads contain...
this->adrepository->findAll() i automcally access ads with selected cateogry because this relationship already exist in my model but i need some how $this->categoryRepository($this->adrepository->finAll())
in drop down list i need only that category which is currently active. I can access this through simple query like i mention above but i need it through extbase query stuff becuse with extbase query way i can use buitin functionaly of typo3 like time stamp for start and end and hidden builtin functionality i want
->statement('
SELECT tx_casmarketing_domain_model_category.uid, tx_casmarketing_domain_model_category.name
FROM tx_casmarketing_domain_model_category, tx_casmarketing_domain_model_ad
WHERE tx_casmarketing_domain_model_ad.ad_cat = tx_casmarketing_domain_model_category.uid ') this is working but i want to conver it thourgh extbase query

To make it work with common repository methods you have to define the object-relations in TCA (and not sure if in model too). Then you can use functions like $query->equals('category',$category) or $query->contains('category',$category).
What is the purpose of your query? You are trying to find all categories that are are assigned by an "ad"?

Related

Yii2: Am I able to use multiple models in a search model?

In Yii2, am I able to include two models (active records) in one search model and display them in a gridview?
For example, I have two tables, "customers", and "customer_contacts".
In my search model I am using Customers as my main model, while I wish to "left join" to CustomerContacts, and eventually display the Customers.name and CustomerContacts.phoneNumber in the gridview (in dataProvider).
Can someone please guide me on this.
Thank.
Option 1:
In the customer model add this function:
Public function getCustomerContact(){
Return $this-> hasOne( CustomerContact::className,[customer_id,id]);
}
Then in your grid view you can easily reference the contact as follows:
customerContact.name
Note that this will only work if there is a one to one relationship between the tables
Option 2: (faster but more challenges)
In the data provider, use a query rather than a model ie $query = new \yii\db\Query();
You can then do all joins etc in the data provider. It is much faster but it requires a little more know-how
you can do this by applying left-join in searchModel as following
$query = Customers::find();
$query->joinWith('customer-contact');
And also make hasOne relation with CustomerContact in Customer.php

Laravel eloquent join multiple table and search data using with() method

I have two table user and user_info. I need to join those table and have to search data from them. It is throwing error as unknown column.I have solution using DB query, Is it possible to do search using with() method in controller and eloquent relationship in model.
Thank you
It's not possible to filter models by their related models attributes using with() - this method only allows filtering related models, not the original ones you're loading.
In order to filter by attributes of related models you should use whereHas() method, e.g. in order to load all users that have country column set to uk in their user_info data you could do the following:
$usersFromUK = User::with('user_info')->whereHas('user_info', function($query) {
$query->whereCountry('uk');
})->get();

how to get the repository item for the particuler order Id in atg?

I have task to get the repository item("its like shipping group and payment group details") for the particular order id you now i got all the repository item in one object..
But the thing is i don't know how to fetch the these repository item (shipping group and payment group ) from this object
This my code I have tried..
Repository connection;
connection=/atg/commerce/order/OrderRepository-->this i putted in my property file
Repository repository = (Repository)getConnection();
RepositoryItem Item = (RepositoryItem)
Repository.getItem(getOrderId());
In this "repositoryItem" object I have all the repository item so I have to fetch all the shipping group and payment group from this object..
Pls hep me out..
Thanks in Advance..
For Orders there are two ways to get to the ShippingGroup and PaymentGroup when your starting point is an orderId
Let's start with the Repository way:
RepositoryItem order = getConnection().getItem(getOrderId(), "order"); //The getConnection().getItem(getOrderId()) method is deprecated. Make sure you pass the itemDescriptor in with your query
List<RepositoryItem> shippingGroups = (List<RepositoryItem>) order.getPropertyValue("shippingGroups");
List<RepositoryItem> paymentGroups = (List<RepositoryItem>) order.getPropertyValue("paymentGroups");
However, for certain objects, ATG provide some helper methods to make things easier. So using the out-of-the-box OrderManager, code is likely to make things easier for you:
Order order = getOrderManager().loadOrder(getOrderId());
List<ShippingGroup> shippingGroups = order.getShippingGroups();
List<PaymentGroup> paymentGroups = order.getPaymentGroups();
You will now have easier access to most things in the shipping groups and payment groups. Keep in mind though that your repository customisations will not automatically be exposed in these and you'll have to extend the Order and OrderImpl classes to do so (and similarly for the ShippingGroup and PaymentGroup.
ATG represents orders at two different levels - as repository items (and hence in the database) and as order objects.
It is strongly recommended that you interact with an order at object level, not repository level.
The OrderManager component provides the API you need to load an order by id.
Order order = orderManager.loadOrder(orderId);
where orderManager is the variable that holds a reference to the /atg/commerce/order/OrderManager Nucleus component.
This API hides behind it all the code needed to load the order and its related data entities from the repository.

Entity Framework code first aspnet_Users mapping / joins

I was wondering with Entity Framework 4.1 code first how do you guys handle queries that involve an existing aspnet_Users table?
Basically I have a requirement for a query that involves the aspnet_Users so that I can return the username:
SELECT t.Prop1, u.Username
FROM Table1 t
INNER JOIN aspnet_User u ON t.UserId = u.UserId
Where t.Prop2 = true
Ideally in linq I would like:
from t in context.Table1
join u in context.aspnet_Users on t.UserId equals u.UserId
where t.Prop2 = true
But I'm not sure how to get aspnet_Users mapping to a class User? how do I make aspnet_Users part of my dbset ?
Any help would be appreciated, thanks in advance
Don't map aspnet_Users table or any other table related to aspnet. These tables have their own data access and their own logic for accessing. Mapping these tables will introduce code duplication, possible problems and breaks separation of concerns. If you need users for queries, create view with only needed information like id, user name, email and map the view. The point is that view will be read only, it will contain only allowed data and your application will not accidentally modify these data without using ASP.NET API.
First read Ladislav's answer. If you still want to go ahead : to do what you want would involve mapping the users and roles and members tables into the codefirst domain - which means writing a membership provider in code-first.
Luckily there is a project for that http://codefirstmembership.codeplex.com/ although its not a perfect implementation. The original is VB, look in the Discussion tab for my work on getting it running in c# MVC.
I'm working with the author on a better implementation that protects the membership data (password, last logged on date, all of the non-allowed data) but allow you to map and extend the user table. But its not ready yet!
You don't really need to use Entity Framework to access aspnet_membership provider accounts. You really just need to create an instance of the membership object, pass in a unique user identifier and a Boolean value indicating whether to update the LastActivityDate value for the user and the method returns a MembershipUser object populated with current values from the data source for the specified user.
You can then access the username by using the property of "Username".
Example:
private MembershipUser user =
Membership.GetUser(7578ec40-9e91-4458-b3d6-0a69dee82c6e, True);
Response.Write(user.UserName);
In case you have additional questions about MembershipProvider, you can read up on it on the MSDN website under the title of "Managing Users by Using Membership".
Hope this helps you some with your requirement.

ASP.NET Dynamic Data : How to Specify Sort Order of Items in Dropdown List

I am using ASP.NET dynamic data for the data adminstration tasks for a Silverlight app that I built. It saved a ton of time not having to write all of the admin screens you typically have to create for end users to manage the data.
One thing I cannot figure out how to sort the items in the drop downs that appear on the screens - either the filter dropdowns on the list views or on the data entry screens.
Do I specify that somewhere in the EDM partial classes or in the ASP.NET DD field templates? or somewhere else?
All I need to do is sort alphabetically by the display value- they appear to be in random order.
thanks
Michael
Use the DisplayColumn Attribute in the System.ComponentModel.DataAnnotations Namespace.
http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displaycolumnattribute.aspx
ex:
[DisplayColumn("LastName", "LastName")]
public partial class Employee
{
}
The answer to your question can be found here, about halfway down the page:
http://csharpbits.notaclue.net/2008/08/dynamic-data-and-field-templates-second.html
In the Cascase.ascx.cd FilterControl and Cascade_Edit.ascx.cs FieldTemplate you will find a method GetChildListFilteredByParent. This returns the values for the filtered DropDownList, but as you will see this list is an unordered list. To add sorting to this list we need to add a Linq OrderBy clause.