I'm relatively new to Yii and would like to experiment a little. I have looked around all over the internet and I have absolutely no idea how to approach this. I have mastered the basics of Yii but I think that this is quite advanced or I'm just being stupid.
I have a MYSQL table with a list of items bought. This table has CUSTOMER_ID as the main field with each CUSTOMER_ID possibly having more than one ITEM's. I would like to do a count BY TRANSACTION_ID and then assess as in line with the following logic:
1) IF myCount = 1 then businessType = 'New';
2) If myCount = 2 then businessType = 'Repeat';
3) If myCount > 2 then businessType = '2+ Repeat';
I'm not sure where and how would be the best way to do this however? afterSave()?? Or in the controller? I have to get the result of the query into a GROUPED CActiveDataProvider object as I feed this data into Highcharts.
My grouping needs to look as follows (From lowest level to highest) :
businessType->store->item->{Count(ITEM), SUM(Cost)}
I am unfortunately not able to add a variable to the table and would really like to achieve this without having to. Any help appreciated!
to do this you will need to use CDbCriteria to group results in a CActiveDataProvider. See here http://www.yiiframework.com/doc/api/1.1/CDbCriteria#group-detail. You'll need to implement it in your controller something like this;
$criteria = new CDbCriteria;
$criteria->select = 'the columns to select, and maybe SUM()';
$criteria->group = 'columns to group by';
etc
etc
$dataProvider = new CActiveDataProvider('model name', $criteria);
Without knowing the exact implementation of your database I can only give a general example, but you should find something like this works for you. I'm not sure how to get the grouping your talking about as businessType->store->item->{Count(ITEM), SUM(Cost)}, as you haven't shown your database and model structure.
Related
I am working on a pimcore project (version 4.4.3) but still pretty new to pimcore itself.
First I made an Object called 'Event' in the admin panel and added a data component -> relation -> Object called 'speakers'.
Now I have a controllerAction which needs to return these speakers, but I don't want them all at once so I wish to add a limit and offset.
The result of $eventClass->getSpeakers() returns an array with objects on which I don't seem to be able to put any filters.
Of course I can filter them after I retrieved all of them, but if possible I would like to filter them in my request.
So my question is, how do I filter the related objects on my object?
I'm afraid that currently you can do it only using SQL. It will look something like this:
SELECT dest_id FROM object_relations_5 where fieldname = 'speakers' and src_id = 123 LIMIT 10;
Where 5 should be your class' id and 123 your object's id. You can join other tables to do more filtering, but it's getting complicated.
Commonly if you have to write custom SQL code, something is wrong with your data model. Maybe your "speaker" class should have a single href relation to "event" - this way you could get speakers listing easily with all filtering you want.
You can use the Listing object for that
$speakerId = 123;
$list = new \Pimcore\Model\Object\Event\Listing();
$list->setCondition("speakers like '%,".$speakerId.",%'");
But you can only filters the speakers with their IDs only. If you want to filter them with some other attributes then you have to make a join with object_relations_ClassID table.
Also have a look at the following link
https://pimcore.com/docs/4.6.x/Development_Documentation/Objects/Object_Classes/Data_Types/Relation_Types.html#page_Filtering-for-relations-via-PHP-api
I'm a beginner to zend framework...until now i was doing pagination by fetching all details from DB..my friend says that it is not a good way for pagination....
he give me the following reference from Zend site...now i have some doubts...
1.what is setRowCount?is it a field in the table?
2.what is item_counts and RowCount?
my DB name is sreejith.
my table name is employee.
the code is:
$adapter = new Zend_Paginator_Adapter_DbSelect($db->select()->from('posts'));
$adapter->setRowCount(
$db->select()
->from(
'item_counts', array(Zend_Paginator_Adapter_DbSelect::ROW_COUNT_COLUMN => 'post_count')
)
);
$paginator = new Zend_Paginator($adapter);
OP code pasted from:
http://framework.zend.com/manual/1.12/en/zend.paginator.usage.html
That example is showing the ability to optimize how the total number of rows is calculated (needed to perform pagination, well to render the UI navigation at least).
As it says, "For example, if you keep track of the count of blog posts in a separate table, you could achieve a faster count query with the following setup:".
Look at the more simple examples passing a Zend_Db_Select to the factory... like: Zend_Paginator::factory() - That will probably be all you'll need.
I'm working on a fairly large project in (object oriented) PHP which includes a webshop where users can buy products and additional licenses and services. Purchasing is a four step process:
Step 1: User chooses a product, and product_id is passed to step 2
Step 2: Fetching and outputting all license types based on product_id, passing product_id on to step 3
Step 3: Fetching and outputting all services based on product_id, in a form with checkboxes named services[$service_id]
So now, on the checkout on step 4, I fetch the correct products and licenses from the database, but I also have to get the correct services from the db based on the services array, and calculate the price to output. At last, I'll have to assign the services array to the Smarty template.
How would the most suitable way to do this be? I really hope that someone is kind enough to help me out here, as I'm having a very hard time trying to make this work.
Thank you so much in advance.
Try using
$resulter = array();
$i = 0;
foreach($product_id as $value) {
$query = "Select FROM products WHERE product_id = $value";
Your execution code
$resulter[$i] = $result; //$result could be a assoc array
$i++
}
And If I ware you I would i would use a multidimensional array like I shown above.
Sounds like you need a JOIN.
Something like a JOIN on the 'products' table and 'licences' table using the 'product_id' field to make the join.
An example query would be something like:
SELECT <required_fields_here>
FROM products
JOIN licences ON licences.product_id = products.product_id
WHERE product_id = <product_id_here>
Note that in the SELECT section you can select fields from both 'products' and 'licences' tables, you just need to prefix with the table and a dot e.g. 'product.product_id'
I think you will need to be more specific if you need further help. Hope it helps.
When inserting products in an eshop we often need to link some products (aka related products) to others and the linking must be done both ways, meaning if I link product1 to product2 then product2 must also be linked to product1.
Which is the best practice, using an extra table 'relations' (prodid, related_prodid) or to keep a list of related products in a delimited string in each product's row in the products table?
In either case, we would also need a recursive method to loop through a given array of products and insert/update the tables with the relations, could someone help me out with the algorithm? I will do the PHP coding but I cant think of a good way to do it.
You'd better use an intermediate table related_to(id, product1, product2)
Then, you'll use the code:
function findRelatedProducts($product) {
$relatedProducts = array();
$data = mysql_query("SELECT * FROM related_to WHERE product1='$product' OR product2='$product'");
while ($relation = mysql_fetch_array($data)) {
$relatedProducts[] = $relation['product1'] == $product ? $relation['product2'] : $relation['product1'];
}
return $relatedProducts;
}
Of course, you need to JOIN this table with your product table, but since I don't have much informations about your mysql structure, I'll let you check on this site if you don't know how.
Definitely use the extra table (the string solution is really a bad idea), preferably organizing it so that the product with the lowest primary key is put first in the relation (allows for a bit of optimization); there is no need to duplicate the relationships (i.e. having and at the same time).
As for the recursive method thing, it's not clear where you get the relations' value from.
Hello i am in a delima
Suppose that i have 50 products in a category and i want to get all the features of my products...
Ofcourse a SQL join wont help because joining would return product info many times!
So here is the question.. what is better
PLAN A
Get all the products' features in category with one SQL query and then iterate with php each feature and put it in Product.
PLAN B
For each product get the features by calling a query
Other solutions accepted!
EDIT
My table schema outline is..
A table Product which has product info(per row)
A table features which has features (feature id )
A table for features' values
And a table that has Products with their features and values
$sql1 = "SELECT * FROM products P, ". //don't use star, make sure no fields are overwritten
INNER JOIN products_to_features PTF on P.id = PTF.project_id
INNER JOIN features F F.id = PTF.feature_id
ORDER BY P.id";
$r = mysql_query($sql1, $conn);
$arr = array();
$lastProductId = -1;
while ($row = mysql_fetch_assoc($r))
{
if ($row[p_id] != $lastProductId)
{
$lastProductId = $row['p_id'];
$arr['p_id'] = array('productName' => $row['p_name'],
'productPrice' = $row['p_price'],
'productFeatures' = array(),
//other fields from product table);
}
$arr['p_id']['productFeatures']['f_id'] = array('featureName' => $row['f_name'], blah...);
}
I don't know your fields obviously, and you may want to join on feature_values so that will be more work. You can do keys/values different (ie - product names as keys. Feature-name as keys with feature-value as values, whatever you want) but the point is this is doable (and recommended) in one query.
Not Plan B.
Whatever you do this can and should be done with one or at most two total queries (one for headers, one for correctly sorted list of features + id column). Whether that query is Plan A or some unmentioned Plan C depends on your exact table structure, which isn't clear from your question.
Generally, the less database queries you have to make, the better. It greatly depends on your table structure, but I'd go with Plan A.
A query inside a loop will greatly degrade performance of your application. Avoid it at all cost.
Show us the schema. There's a strong possibility a single SQL query will solve your problem.