Yii Grid Calendar Spreadsheet Form - yii

I'm a beginning PHP developer. I have an app I've written that I would like to rewrite using Yii. Most of it is pretty straight-forward so far but I have one view in the current app that the company using the app loves and I doubt they will want it to change.
In this view there is a form that looks a lot like a spreadsheet (lots of rows and columns to input quantities). The first column of the form is the name of an item, the other 7 columns represent the quantity of the item for each day of the week. Each cell represents an order for the row item and column date and has an input box.
In the database, each individual order is a row in the database and there is only one date column for each order obviously. Rigging up the view was easy enough in pure PHP, but I have no idea how to do this with Yii.
Yii's CGridView seems to map database columns to columns in the data grid table. I can't figure out how to have 7 columns in the grid view, one for each day of the week when the database only has one date column. According to the documentation in CGridView each row corresponds to one data item. In my view each row would need to be 7 data items.
The question(s):
Is there any way to do this with Yii right now?
Would writing an extension be a good solution?
Is Yii not going to help me at all with this view? Or is there something besides CGridView?
If Yii can do it, where do I start? Something in CActiveDataProvider, extending CDataColumn, tweaking the model?
I'm not really sure how to proceed, and don't want to get too far into porting the app to Yii if it's not appropriate. Hopefully this was clear enough to convey what the issue is. Thanks in advance.

One option would be to create some relations for your items table. I assume they are already related to the order table. You could create a STAT relation for each day of the week in your items model, for example:
public function relations()
{
return array(
...
'sundayOrderCount' => array(self::STAT, 'Orders', 'item_id', 'condition'=>'DAYNAME(`order_date`) = "Sunday"',
...
);
}
(where item_id is the fk for Items in your Orders table, and order_date is a timestamp in your Orders table of when the order was made)
In the example above you should be able to grab all the items from the table and get each items total sales for Sunday like so
$model->sundayOrderCount;
And pop in a CGridView like so:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'item-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'name',
array(
'header'=>'Sunday Sales',
'value'=>'$data->sundayOrderCount',
),
),
));
Not tested it, but I hope that gives you something to go on.

Related

How do I correctly use report-level filters in Power BI?

I am fairly new to Power BI and working on my first project. I currently have two separate sql views setup and connected to my project as well as a simple visual for each one. They both share a common field that I want to filter on, call it customer_key. I tried adding a filter to the "Filters on all pages" section but it doesn't seem to filter both visuals, only the one I dragged the field from. I ultimately want to do the same thing with a date field so that this report can be easily re-ran for any of our customers and any date range. Suggestions?
I would like to have a single filter that affects all data sets in my report, provided they have that column to filter on. Currently, I am using separate filters for each data set (even though I moved the filter to the "Filters on all pages" section).
This happens when the "common" customer_key columns are not related. So you have to set up a relation between your two sql views. However, if both are fact tables this is not straight forward, since you would end up with a many-to-many relation.
Workaround: Extract the unique customer_key values from both views and create a third dim table from it. Now you can create two one-to-many relations to your sql views and use the third table customer_key as a report-level filter.
You can create the connecting dim table with the following DAX table expression:
Table3 =
DISTINCT(
UNION(
SELECTCOLUMNS(
'Table1',
"customer_key", 'Table1'[customer_key]
),
SELECTCOLUMNS(
'Table2',
"customer_key", 'Table2'[customer_key]
)
)
)

Triple relation QueryBuilder symfony (what the hell am I doing?)

Let me explain how I am making things (maybe not the best way by the way).
I want to join my StoreSchedule entity (which contains a triple relation : store (which is where I stock all informations about adress, name, picture of my stores), days (the 7 days of the week) and Schedules (only strings like '09:00-22:00').
To combine thoses 3 entities, I made StoreSchedule, that has a triple relation where I cross the 3 informations. Maybe I am not explaining well, let me show you some screens.
What I already tried to do with my QueryBuilder in my repository:
https://imgur.com/a/bE52iBH
How I structured things in my StoreSchedule table :
https://imgur.com/a/6m7YkhI
My Schedule table :
https://imgur.com/a/75bRriS
Days table contains the 7 days of the week. Maybe that's obvious, maybe not.
So here's the thing, I can't figure out how to make the I need : a query that gets all content from Store and joins StoreSchedule where ID = Store.ID
I want to get days and Schedule with the ID from store.
Can I do that with query builder?
Do I have to modifiy my database? Are my relations good?
Best regards!
ps : hope I made myself clear enough..
You make it too complicated. You can purge the days table AND StoreSchedule. Just make a many-to-one relation between Store and (Store)Schedule. Add a column "day" in your schedule table. A short integer will do if you store the number of the day. See php's date function.
For your query (the first link) you won't need that WHERE clause. Doctrine will join the related data automatically for you.
public function getAllContentStoreAndSchedule()
{
return $this->createQueryBuilder('st')
->leftJoin('st.schedule', 'sc')
->addSelect('sc')
->orderBy('st.name', 'ASC')
->addOrderBy('sc.day', 'ASC')
->getQuery()
;
}

Few questions about Grails' createCriteria

I read about createCriteria, and kind of interested on how these works, and its usability in providing values for dropdown box.
So say, i have a table in the database, Resource table, where i have defined the table in the domain class called Resource.groovy. Resource table has a total of 10 columns, where 5 of it are
Material Id
Material description
Resource
Resource Id
Product Code
So using the createCriteria, and i can use just like a query to return the items that i want to
def resList = Resource.createCriteria().list {
and {
eq('resource', resourceInstance)
ne('materialId', '-')
}
}
Where in the above, i want to get the data that matches the resource = resourceInstance, and none of the materialId is equal to '-'.
I want to use the returned data from createCriteria above on my form, where i want to use some of the column on my select dropdown. Below is the code i used for my select dropdown.
<g:select id="resourceId" name="resourceId"
from="${resList}"
disabled="${actionName != 'show' ? false : true}" />
How do i make it so that in a dropdown, it only shows the values taken from column Product Code? I believe the list created using createCriteria returns all 10 columns based on the createCriteria's specification. But i only want to use the Product Column values on my dropdown.
How do i customize the data if in one of the select dropdown in my form, i wanted to show the values as "Resource Id - Resource Description"? The values are combination of more than 1 columns for one select dropdown but i don't know how to combine both in a single select dropdown.
I read that hql and GORM query are better ways of fetching data from table than using createCriteria. Is this true?
Thanks
First of all refer to the document for using select in Grails. To answer all questions:
Yes, the list to select from in the dropdown can be customized. In this case it should be something like from="${resList*.productCode}"
Yes, this can be customized as well with something like
from="${resList.collect { \"${it.resourceId} - ${it.resourceDesc}\" } }"
It depends. If there are associations involved in a domain then using Criteria will lead to eager fetches which might not be required. But with HQL one gets the flexibility of tailoring the query as needed. With latest version of Grails those boundries are minimized a lot. Usage of DetachedCriteria, where queries etc are recommended whereever possible. So it is kind of mixing and matching to the scenario under consideration.

Adding lookups to given view

I'm just getting into Yii. I have a simple relational db. I have a "client" table related to "orders" table (client:id to orders:client_id). If I build my CRUD for orders i naturally see client_id, however I'd rather add a lookup for client name somehow
I have found how to do this on new and update _forms by adding a dropDownList. The list based views seem a little more complex. I can see that actionIndex() in the controller is gathering data and passing it to index.php, finally through to _view, but I can't find any help on where and how I should break into this with my lookup returning the client name
I'd appreciate any help
thanks
Check the Yii documentation about relations. You will need to create a relation in your orders table, lets call it client, Then in your list view where it generates client_id you can put client.name instead. Now you will need to make sure that you have the appropriate label because in the generated model it will only have a label for client_id, and you need a label for client.name. Yii will guess, or you can add it, or you can modify the label for client_id, and instead of using client.name in the view you can use
array(
'name'=>'client_id',
'value'=>$model->client->name,
)
I tend to gravitate towards more explicit definitions rather than shortcuts, but to each his own.

Rails 3 - defining :order using a combination of attributes, while treating the *latest* record differently

My Application has the following :order requirements:
Order by the importance (measured by say number of likes) AND
Order by created_at DESC
So I am currently using the following to define the :order
:order => 'model.likes_count DESC, created_at DESC'
However, in my views, I create new model entries using AJAX and therefore, I reload the partials where I display the database entries for this particular model and would like to use jQuery effects to (say) highlight the record that was just created.
Now due to the above :order definition, the record just created would not show up as the first one if an older record has greater number of 'likes'.
Is there a way to define an order which takes into account the "latest" record differently and then order the rest of the records as per the defined order? If possible what would be the clean way to achieve this.
Thanks!
The Rails API does not have any functionality to treat a newly created record differently.
Your options are:
Do not select the new record in the query, eg. with .where("id <> ?",idofnewrecord)
You can select the new record in a separate query if required
Or, select the records as normal, then find the new record in ruby, eg:
#newrecord = #records.find{|r|r.id==idofnewrecord}
or to delete it from the array of records:
#newrecord = #records.delete_if{|r|r.id==idofnewrecord}
If you don't know the id, you may be able to filter it out with some other attribute as well, eg. created_at.