Yii - using MySQL AS clause field - mysql

Let's say I want to have a provide CActiveDataProvider for a CGridView. I need to put a SUM(invitesCount) AS invites into a Provider result. How to retrieve it? I guess I cannot just use $dataProvider->invites?

You need to specify the following in your relationinvites
'invites '=>array(self::BELONGS_TO, 'CampaignFund', 'campaign_id', 'select' => 'SUM(invitesCount)'),
and use this relation in your criteria.

Several other options:
Use CStatRelation
invitesCount=>array(self::STAT,'Invites','foreign_key_field');
The addition of a public property can work. However, the field would only be set if you altered the default find query to include this new condition. This can be done by overriding defaultScope() or creating a new scope and using it whenever invitesCount is required.
Another option would be to create a database view from the required query and create a new Model from that database view.

Related

Send all records to server using Ext.data.store of extjs5

I want to send all (inclduing unchanged) records of an Ext.data.store using the sync-method to the server.
In extjs4 you coud do the following (answer from this question):
store.each(function(record){
record.setDirty();
});
store.sync();
But in extjs5 the setDirty()-method is deprecated, and I wasn't able to find an alternative solution.
You could override getModifiedRecords to always return all store records.
You could also add extra field into model, not stored anywhere, and change it before sync.

CakePHP virtual field: replace one string with another

I wonder if there is any way to declare a virtual field in CakePHP to do the following:
We have to replace a user's status with a symbol and append to it the user's nickname. For example, if a user is an admin, we want to display: #barth, for a regular user ~barth.
I already wrote an afterFind() callback to perform this task, but it fails using the containable behavior.
Either is there another way to implement it, or we can create a virtual field. The latter solution would be very elegant, but after googling I cannot find any way to use MySQL syntax to replace one string with another.
Ideas?
Virtual fields are very easy to use in Cake. You can use any regular MySQL function in their declaration to achieve this type of thing.
You'll first need to determine the SQL command to achieve what you want, I'd suggest using the CONCAT() function:
-- Return an # concatenated onto the username
CONCAT('#', yourfield)
Then add this as a virtual field in your model:
class YourModel extends AppModel {
public $virtualFields = array(
'yourVirtualField' => 'CONCAT("#", yourfield)'
);
}
Now, when you query this model you should be able to access it like this:
$example = $this->YourModel->find('first');
echo $example['YourModel']['yourVirtualField']; // #yourfield
Edit
Since your update, you've got the values you want to concatenate together in another model as virtual fields already. CakePHP doesn't allow you to use associated models' virtual fields when creating a new virtual field, but you can do a subselect query to manually get this data. Here's an SQL Fiddle example.

Sequelize (v1.5) and node

How can I check whether a field exists in my table, via sequelize orm.
Note that I have already defined the full object model. I just need to check whether a particular field exists or not.
You can see what is inside your database via:
sequelize.getQueryInterface().describeTable('nameOfTableHere').success(function(data){})
If you want to check the table of a specific model you could also do this:
sequelize.getQueryInterface().describeTable(Model.tableName).success(function(data) {})
Since I had already defined the object model, the following expression gives an array of field names defined in the model.
Object.keys(Model.rawAttributes)

Django - Add rows to MySQL database

So I already have a database setup with a few columns and a few rows already inserted in. I'm trying to create a view that you would just input information into a form and press Submit, then a row would be added to the MySQL database with the information you just typed in.
I believe you can do this with admin, but I would like to try without admin and I'm not sure if this is possible? I've been using the MySQL commandline to add rows as of now..
Of coures this is possible this is a building block for data driven websites. You can use a ModelForm as Daniel suggested (they offer built in validation and HTML markup for FREE) to easily map your model to a front end form. It would probably be beneficial to start with django tutorial or documentation first.
At the the very basic, all you have to do is instantiate your model
new_entry = YourModel(name='me', age='222', about='stackoverflow')
then save it
new_entry.save()
This adds it as a new row to your db.
https://docs.djangoproject.com/en/dev/topics/db/models/
Why would it not be possible?
You probably want a modelform (but see the general form introduction first).
Try out this example of Generic Views: http://postneo.com/2005/08/17/django-generic-views-crud (assumes a model named Task)
With Generic Views you get Insert, Update and Delete for free without any real work. give it a try and let me know what you think.
from django.conf.urls.defaults import *
info_dict = {
'app_label': 'tasks',
'module_name': 'tasks',
}
urlpatterns = patterns('',
(r'^tasks/create/?$', 'django.views.generic.create_update.create_object', info_dict ),
(r'^tasks/update/(?P<object_id>\d+)/?$', 'django.views.generic.create_update.update_object', info_dict),
(r'^tasks/delete/(?P<object_id>\d+)/?$', 'django.views.generic.create_update.delete_object', info_dict ),
)
Django Docs: https://docs.djangoproject.com/en/1.2/ref/generic-views/#create-update-delete-generic-views

Storing extendable options list in MySQL table? Best practice?

Let's suppose that we have multi-site CMS and every website in this CMS having 2 options: preview_width and preview_height. We know that every option should have default value if isn't defined by user. Also we know that list of options will be extented in near future. What is the best practice to store such options in MySQL table?
I know three practices and both of them have lacks (or maybe I don't know how to correctly use this practices)...
Practice #1: Each option is
represented as column in options
table.
Disadvantage: We should
modify options table each time
we're adding new option.
Practice #2: All options are stored
as serialized object/array in
options column of sites table.
Disadvantages: To add new option
with default value - we need to loop
through all rows and modify
serialized options; or we can add
this option when it is requested and
found not present.
Practice #3: All options are stored
in options table with structure: id,
site_id, option_name, option_value.
Disadvantages: When adding new option we should update this table with default-valued options for each website.
What is your choice?
What practice to choose when new options are added very often?
Any other practices?
Thank you.
I would use Practice #3. In order to store default value you can try writing a method to get options:
get_value(option) {
value = read_from_db(option);
if value == not_present_in_db {
value = default_value(option);
}
return value;
}
You also need to write default_value(option) method which should look for some defaults in a configuration file or whatever.