I am a newbie with ASP.NET MVC 5 design-Pattern. Kindly look at my problem and please help me in following scenario.
Here it is my table structure:
Order
( Id
| CustomerName
| OrderDate )
OrderDetail
( Id
| OrderId
| ProductId
| Quantity )
Product
( Id
| Name )
Order can have more than one detail, mean while detail has reference to product table. I want to create a screen where user can create new order, select products and also define quantity against each product.
I was not able to get a start point for create screen of this scenario in MVC Pattern.
Thanks.
Related
today i have an interest task - realize search on DB by some groups and sort it by most relevant criteria
We have 2 entities (User and Interests) on Symfony with ManyToMany relation
We need create query for find users which have most similar interests
ex:
user1 have interests [1,2,3,4,5]
user2 have interests [1,2,4,5,7]
user3 have interests [3,5]
we try find user with interests [2,6,7] and result must be:
[user2, user1, user0]
user2 - 2 similar interests
user1 - 1 similar interests
user3 - 0
Code example:
class User
{
// ...
/**
* Many Users have Many Interests.
* #ManyToMany(targetEntity="Interest")
* #JoinTable(name="users_interests",
* joinColumns={#JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={#JoinColumn(name="interest_id", referencedColumnName="id")}
* )
*/
private $interests;
...
I have no idea how to organize it nicely, can somebody help me?
Thanks!
One way of doing it
Find user with at least 1 interest of your list (Simple Dql Query)
Count number of common interest in php.
For example using count(array_intersect($userInterestsIds, $wantedIds))
You have all the wanted info, display it as you want
I suggest you to use simple sql (not DQL) in order to extract only the id of the user that match the criteria.
A simple query could be:
select distinct user_id, count(*) from users_interests
where interest_id in (1,3,5) --- your interest ids
group by 1
order by 2 DESC;
Hope this help
My Situation
I'm using Advanced Custom Fields Pro to store metadata for a post in wordpress.
My posts are created dynamically (not through the administrative interface), which means that I explicitly need to populate metadata using field keys, not field names. One of my fields is a repeater field with a single text area and another is a standard text area.
My Code
The following code is what I call (once per post). The post is created using wp_insert_post() earlier.
// Populate "Name"
update_field('field_566e360961c2f', 'John Doe', $wp_identifier);
// Populate "Sponsors"
foreach($sponsors as $sponsor) {
// Define "Sponsor Name"
$new_sponsor = array(
'field_566e32fb943a5' => 'Beats and Corn Companye'
);
add_row('field_566e32bd943a4', $new_sponsor, $wp_identifier);
}
The result of this is that standard text fields populate, and a single "sponsor" repeater item is created, but the value of the sponsor name is blank.
The relevant wp_postmeta data that is generated looks like this:
| 18226 | 71 | name | John Doe
| 19234 | 71 | sponsors | 1 |
| 19235 | 71 | _0_field_566e32fb943a5 | Beats and Corn Company |
My Question
What am I doing wrong? Looking at the documentation for add_row() this appears to be the correct approach. Is it possible that repeater fields have a different way of notating keys that I'm not aware of?
This isn't made incredibly clear in the documentation today, but it turns out add_row only works if an existing row had already been saved. When trying to populate a repeater field for the very first time you have to use update_field instead and pass an array of value arrays.
The corrected code:
// Populate "Name"
update_field('field_566e360961c2f', 'John Doe', $wp_identifier);
// Populate "Sponsors"
$new_sponsors = array();
foreach($sponsors as $sponsor) {
// Define "Sponsor Name"
$new_sponsor = array(
'field_566e32fb943a5' => 'Beats and Corn Companye'
);
$new_sponsors[] = $new_sponsor;
}
update_field('field_566e32bd943a4', $new_sponsors, $wp_identifier);
Although I know this is trivial I'm stuck trying to implement the pagerfanta Paginator using the DoctrineORMAdapter, I want to paginate all entities sorted by id in descending order, the final SQL I want is this:
SELECT id, name FROM User ORDER BY id DESC LIMIT 0, 5;
Suppose I had users from A to Z and I want to limit them by 5 each page, what DoctrineORMAdapter is paginating results in User E to A listed in the first page, while what I actually expect is to see User Z to user V in the first page, U to Q in the second page and so on. The DQL I'm passing to DoctrineORMAdapter is as follow:
SELECT u FROM My\FluffyBundle\Entity\User u ORDER BY u.id DESC
On execution this is the final DQL for the first page:
SELECT DISTINCT id0 FROM (SELECT u0_.id AS id0, u0_.name AS name1 FROM User u0_
ORDER BY u0_.id DESC) dctrn_result LIMIT 5 OFFSET 0
Please note that when using the ArrayAdapter instead of DoctrineORM's it works as expected, but it's not a good idea to rely on ArrayAdapter when you have thousands of complex Doctrine Entities, not even with extra lazy loading :D.
This is the only relevant code:
$queryBuilder = $repo->createQueryBuilder('u')->orderBy('u.id', 'DESC');
$adapter = new DoctrineORMAdapter($queryBuilder);
$pager = new Pagerfanta($adapter);
$pager->setMaxPerPage(5);
Thanks.
This will help you:
$adapter = new DoctrineORMAdapter($queryBuilder, false);
Had the same problem this morning.
By default Pagerfanta is treating your query as one with joins. Setting second argument to false makes it use simple query handling.
In Kunstmaan Bundle, in AdminListConfiguration class, you have to overide function that is creating Pagerfanta, if you want to sort simple entity.
I was trying to do a 'Latest Products' page for an eShop project I'm working on. Basically, I want to show a number of products, say 10, from a table in my db that where added in my database in the last 30 days or less.
First I tried to use the GridView function in VB.NET, where it auto-populates the table but can't be limited and then I tried this SQL statement, which isn't working, giving me an error.
SELECT *
FROM Product
WHERE DateAdded > (SELECT DATEADD(d,-30,(SELCT MAX(DateAdded) FROM Product)) AS "Last 30 Products Added");
ORDER BY DateAdded DESC
My database has a table called 'Product' which has various columns:
ProductID
CategoryID
ProductModelNo
ProductImage
ProductName
UnitName
ProductActive
DateAdded
Any idea on how I can solve the problem ?
You are looking for the DATEDIFF function to compare the DateAdded with NOW().
In a table I have news posts with these fields:
Title
Content
OwnerID
And a users table
ID
Name
Surname
The OwnerID relates to the ID in the users table, how can I get the name of the user who's ID matches the OwnerID?
I'm writing a website in ASP.net (VB).
You would need to join the two tables together like this:
select users.Name
from news inner join users
on OwnerID = ID;
This query has no where clause to filter the results that are returned so this query would return all users who are associated with a news record. If you wanted to find users associated with a specific news record you would need to filter on the news title or content like this:
select users.Name
from news inner join users
on OwnerID = ID
where Title = 'Some Title';