How to load whole table into a model? - atk4

I have a database table and I want to load it to model? Not by specified condition but whole table. How to do it?

models in ATK4 are used to represent data and to abstract the underlying technicalities but not directly hold them.
it is used by MVCGrid, MVCForm and CRUD to know how data is to be presented / laid out to the user's interface.
though models may not hold data, they are used to retrieve it through dsql.
example:
$m = $this->add('Model_UserAccess');
$u = $m->dsql()
->field('usernm')
->field('acclvl')
//->do_getOne(); // return only 1 record
->do_getAll(); // return all records
the use of DSQL or Dynamic SQL is important
to retrieve data from tables and save it back.

I have a very simple script that creates the necessary .php files to copy to /lib/Model.
I did it because was migrating an Access application with tables of 20-30 attributes and was very tedious to create it by hand.
The script it's very very basic, but it's very useful for me.
Here is the link
https://github.com/ajmmartinez/atk4_create_mod

Try
$data = $mymodel->getRows();
This will get all the data from the model and store into the array. Your question is difficult to understand, so I'm not sure how to reply.

Related

Can I run or execute dbt model based on output from a SQL statement?

Background: I have few models which are materialized as 'Table'. This tables are populated with wipe(Truncate) and Load. Now I want to protect my existing data in the Table if the query used to populate data is returning empty result set. How can I make sure an empty result set is not replacing my existing data in table.
My table lies in Snowflake and using dbt to model the output table.
Nutshell: Commit the transaction only when SQL statement used is returning Not empty result set.
Have you tried using dbt ref() function, which allows us to reference one model within another?
https://docs.getdbt.com/reference/dbt-jinja-functions/ref
If you are loading data in a way that is not controlled via dbt and then you are using this table - this is called a source. You can read more about this in here.
dbt does not control what you load into a source, everything else that is the T in the ELT is controlled where you reference a model via ref() function. A great example if you have a source that changes and you load it into a table and make sure that incoming data does not "drop" already recorded data is "incremental" materialization. I suggest you read more in here.
Thinking incremental takes time and practise, also it is recommended every now and then to do a --full-refresh.
You can have pre-hooks and post-hooks that can check your sources with clever macros and add dbt tests. We would really need a little bit more context of what you have and what you wish to achieve to suggest a real response.

How does SQLITE DB saves data of multiple tables in a single file?

I am working on a project to create a simplified version of SQLite Database. I got stuck when trying to figure out how does it manages to store data of multiple tables with different schema, in a single file. I suppose it should be using some indexes to map the data of different tables. Can someone shed more light on how its actually done? Thanks.
Edit: I suppose there is already an explanation in the docs, but looking for some easier way to understand it better and faster.
The schema is the list of all entities (tables, views etc) (the database as a whole) rather than a database existing of many schemas on a per entity basis.
Data itself is stored in pages each page being owned by an entity. It is these blocks that are saved.
The default page size is 4k. You will notice that the file size will always be a mutliple of 4K. You could also, with experimentation create a database with some tables, note it's size, then add some data, and if the added data does not require another page, see that the size of the file is the same. This demonstrating how it's all about pages rather than a linear/contiguos stream of data.
It, the schema, is saved in a table called sqlite_master. This table has columns :-
type (the type e.g. table etc),
name (the name given to the entity),
tbl_name (the tale to which the entity applies )
root page (the map to the first page)
sql (the SQL used to generate the entity, if any)
note that another schema, sqlite_temp_master, may also exist if there are temporary tables.
For example :-
Using SELECT * FROM sqlite_master; could result in something like :-
2.6. Storage Of The SQL Database Schema

Cake2 - show data from 2 identical databases

i have quite large application with complex DB structure (lots of tables with foregin keys.. ).
This application exists in 2 instances ( so there are 2 schema-identical DB's ), every has different data (some data can be same like 'district').
I need to create third application for reading data only. This third application needs to read data from both databases, merging them and displaying them.
I need to by able to use existing application code for reading, sorting, filtering, paginating.
What is most effective way to achieve this in cakephp?
Thanks in advance
Possible solution:
- create your own data source ( extend existing Mysql )
- overload "read" method, in this overloaded method, merge records and return

Saving data to a different database table using Cakephp model::save()

I am a newbie to cakephp and could really use some help and suggestions!
the application I am working with currently interacts with two databases, both databases have more or less a similar schema and table structure, I have to save some information in both databases, so i have this table say "employee_information" in both databases, both tables have a set of common fields (first_name, last_name, birthday, gender etc) and some other fields specific to that database.
now i have to save some information into the other database using cakephp model::save() method, previously I was normally switching data source and would use sql INSERT to do this and it was working fine, but now i really would like using cakephp conventional methods to do this, reason is that i think i am missing a great deal by not using cake's own methods ( data sanitizing in my case)
i had tried switching data source and using model::save(), the method did not work, though it did not log any errors, but also did not add any record into the database.
// using following snippet in the model to save.
$this->setDataSource('secondary_database');
$this->save($this->data);
$this->setDataSource('primary_database');
Any ideas or suggestions would be highly appreciated!
Thanks!
You're almost there, but you need to setup two db configs and select them with useDbConfig
For example:
$this->User->save($this->data); //Saves data to default (first) database
$this->User->useDbConfig('second'); //Selects second database for next uses
$this->User->save($this->data); //Saves data to second database too
//$this->User->useDbConfig('default'); //Not needed unless you want to do staff with the default database again later in the same code.
But if I'd need to save different fields in each DB, then I'd go with different models.
Setting custom table for the controller after switching data source worked for me. (http://api.cakephp.org/1.3/class-Model.html#_setSource)
$this->User->setDataSource('secondary_database');
$this->User->setSource('secondary_database_table');
$this->User->save($this->data,array(
'validate' => true,
'fieldList' => $fieldList // specific fields that needs to be updated.
));

Is there any overhead with LINQ or the Entity Framework when getting large columns as part of an entity?

Let's say you have a table containing articles and you want want to display a list of them, excluding the actual article text. When you get a list of the article objects using LINQ or the Entity Framework, is there a LOT of overhead associated with getting that text column too? I assume that when you start enumerating the list, the article text will be stored in memory until the objects are disposed of.
So would it make sense to create an intermediary object that doesn't contain the text column? If so, how would you do this? Make a class inside your DAL, allow the ORM to automatically create one by setting up a stored procedure, or some other process?
The overhead isn't huge (just the cost of sending the data over the wire), but if you don't need the data sure, don't return it. I find the easiest way is to use anonymous types:
from a in Context.Articles
select new {Name = a.Name, Author = a.Author};
Since you're not actually materializing any Article instances, the Entity Framework won't need to fill out all the properties of an instance.
If you don't need the data you should definitely create a different type. By convention I typically name this sort of class "nnnInfo" or "nnnListItem". To create ArticleListItem, in L2S, simply drag the table to your DataContext designer a second time. Then rename it from 'Article1' to 'ArticleListItem' and remove the unneeded properties (rt click, delete). In EF, the process would be similar. As Craig notes, you could use anonymous types, but by creating a concrete type, you can reuse throughout your app, expose via services, etc.
A second way to do this would be to create the class manually and write an extension method to return ArticleListItem:
public static IQueryable<ArticleListItem> ToListItems(this IQueryable<Article> articles)
{
return from a in articles select new ArticleListItem{ Title = a.Title, ...}
}
This would allow you to "cast" any queries against Article as ArticleListItem...

Resources