Fetching data from multiple tables in yii - yii

I have 2 tables
AutoScriptArgumentClass
id
ScriptArgumentClass
AutoTestScriptMeta
id
AutoTestScript_id
ScriptArgumentClass_id
here is my query.
select sac.id,sac.ScriptArgumentClassType
from AutoScriptArgumentClass sac,AutoTestScriptMeta tsm
where tsm.ScriptArgumentClass_id = sac.id
and tsm.AutoTestScript_id=129
how do I write this in yii
I would like to have in this manner:
**$data=AutoTestScript::model()->findAll('Category_id=:parent_id',
array(':parent_id'=>(int) $_POST['TestCaseCategory']));**
$data=CHtml::listData($data,'id','ScriptName');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
Thanks

I am afraid this will not be possible, as you are trying to get the data from multiple table into an instance of a CActiveRecordModel of a particular table.
This model does not have the instances of the columns of the other table, hence it cannot not avail them to you.
Thanks.

Its possible in two condition. first you write a function in in your controller which contain the proper query and call the function from cgrid view or you can check the page more efficient way
http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/
make sure the tables maintain the reletaionship. if your tables are in MYISAM format change it into INODB and try.. hope its works for you

Related

Bigquery request on multiple tables

I would like to request on several tables with the following format name :
project.dataset.fr_table_20221001
project.dataset.fr_table_20221002
...
project.dataset.fr_table_20221030
project.dataset.en_table_20221001
project.dataset.en_table_20221002
...
project.dataset.en_table_20221030
etc with multiple langages and other dates.
Do you know what is the good syntax? Thank you in advance
By request did you mean retrieving table names on a specific dataset?
Can you try this query:
SELECT
CONCAT(table_catalog,'.',table_schema,'.',table_name) as Result
FROM
cars.INFORMATION_SCHEMA.TABLES;
you can change cars to your dataset to view the list of tables from yours. Please see sample output:

Is it possible to use a my SQL inner join query inside my ASP.NET WEB API app directly, or is it better to translate, if so how can it be done?

I'm working on my first (kinda) big personal project and I am stuck. I have 4 tables, 3 of which have foreign keys linking into tbl_model_details. All tables are listed below.
tbl_model_details
tbl_model_type
tbl_model_name
tbl_model_scale
Ideally I want to show data through my controller with HTTP Get. I can get Postman to to return data from my controller using _context.tbl_model_details.ToList();
Currently Postman is showing the id's for the other tables, but want them to show data from other columns within those tables instead of the id.
Within SQL I was able to build this query which displays the information I would like from the other tables, Is there an equivalent that I could make to run inside my controller? Or is there a way I can use this query that I have already made?
SELECT model_scale, model_name, info, picture, model_type, part_number, amount_owned, modified, limited_addition, date_purchase, price_paid, upc
from tbl_model_details
join tbl_model_type
on tbl_model_details.type_id = tbl_model_type.type_id
join tbl_model_name
on tbl_model_details.name_id = tbl_model_name.name_id
join tbl_model_scale
on tbl_model_details.scale_id = tbl_model_scale.scale_id
Any help from you guys would be great.
Thanks
You can use Entity Frameworks LINQ Include. This will allow you to include the sub-models in the same query:
_context.tbl_model_details
.Include(details => details.tbl_model_type)
.Include(details => details.tbl_model_name)
.ToList();
Without knowing your relationships, DBSet and Model setups, I can say that the statement will look exactly like the one I mentioned, but this may help you get on the right track.
This will allow you to later retrieve data from the sub-models:
#Model.tbl_model_scale.model_scale;

Xcart - How to delete rows from table in Xcart

I am using X-Cart for my project, I need help in how to perform delete query in X-cart. Currently, I am using the below code but it showing me an error.
Can anyone help me in this??
$products = \XLite\Core\Database::getRepo('\XLite\Module\XCExample\FormDemo\Model\NewScroll')->createQueryBuilder('p');
$products->delete('xc_news_scroll p');
$products->getResult();
If you want to remove some object, then you should go:
\XLite\Core\Database::getRepo('\XLite\Module\XCExample\FormDemo\Model\NewScroll')->delete($newScrollObjectToRemove);
If you want to remove some column from the database schema, you need to edit the model class of the entities stored in the table and remove this property there. After that, you will need to re-deploy the store.
Best,
Tony

Simple.Data Eager Load SimpleQuery object

It appears that Simple.Data will lazy load by default.
I simply want Simple.Data to query the database and put the results in an object. For example, as soon as this piece of code is executed the results from the database should be stored in employeeData and the database should not be called again:
var employeeData = db.Employee.FindAllByEmployeeId(employeeId) .Where(db.Employee.EmployeeId == 1);
How do I do this? The Simple.Data documentation only describes how to eager load joins. I do not require any joins, simply to get results from a table when I choose to. If I include this WithEmployee() it will do a left join on the Employee table and output the same data twice...
Figured it out... turns out I can just create a list from the output which will save the data
eg.
.ToList<Employee>()

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.