Using migrations in codeigniter - codeigniter-2

I have this query, I made a migrations file to make table structure for users table in my database but I do not understand how can I insert one row using this migration file because there has to be at least one user in the table. Kindly help

Good Day,
You would first run your migrations which will create the table(s). Then run insert queries to insert your basic information that you need. I think the easiest way to do this is to use the database class and active record.
$insert_data = array(
'vchFirstname' => 'Donald',
'vchLastName' => 'Duck'
);
$this->db->insert('cartoons',$insert_data);
You can read more about this here: http://ellislab.com/codeigniter/user-guide/database/active_record.html#insert

Related

Generate alter table scripts using sqitch

Lets say I create a table in snowflake called Employee with EmployeeID and EmployeeName. I would check in the Create table script for this table to my github repo and get this deployed using sqitch. If in the next release, there is a need to add a new column to this table, can I accomplish this by just adding a new column to the existing Create table scripts. Does sqitch have the ability to compare github and my target DB to understand and generate ALTER scripts in this case?
Never mind, I did some more research and figured that Sqitch follows the imperative approach. Which means that it needs us to write the Alter statements in this case.

Laravel how to move data from table to another

Is there any simple way to move data from table to table using the id? I want to get all the info inside the first table using the id, insert it to another table, and then delete it from current one using Laravel.
Assuming both tables have the same schema you can do:
TableB::create(TableA::find($id));

Laravel Migration create table from existing table

I am working on such application where table audit should perform to make revision of updated records. Let's say for example I have to log each field update in Users update.
I wants to create clone of existing user table by
CREATE TABLE users_audit LIKE users;
My main question is,
Is there any alternative way or provision in Laravel migration to create table from existing table ?
I know we can run raw sql query in migration up/down method using \DB::raw();
But it would be helpful if any helper function available to create table from existing table like, create or table
Yes, you can, see the below link
Click this link
This statement is used to execute raw queries
DB::statement('CREATE TABLE tablename LIKE existingtablename');

updating data from one to table to another table which are in two different schema

I got two schema A and B. In A there is a table 'company' and in B there is a table 'newcompany'. Now I need all the data which is residing on A.company to be updated on B.newcompany whenever there is some data updation on A.company.
Please help me out with a query or some function which implements this functionality.
You could give grants to the user and simply access the table as B.newcompany inside an onupdate trigger for the A.company table.

How should I manage my many-to-many relationships?

I have a database containing a couple tables: files and users. This relationship is many-to-many, so I also have a table called users_files_ref which holds foreign keys to both of the above tables.
Here's the schema of each table:
files -> file_id, file_name
users -> user_id, user_name
users_files_ref -> user_file_ref_id, user_id, file_id
I'm using Codeigniter to build a file host application, and I'm right in the middle of adding the functionality that enables users to upload files. This is where I'm running into my problem.
Once I add a file to the files table, I will need that new file's id to update the users_files_ref table. Right now I'm adding the record to the files table, and then I imagined I'd run a query to grab the last file added, so that I can get the ID, and then use that ID to insert the new users_files_ref record.
I know this will work on a small scale, but I imagine there is a better way of managing these records, especially in a heavy-traffic scenario.
I am new to relational database stuff but have been around PHP for a while, so please bear with me here :-)
I have primary and foreign keys set up correctly for the files, users, and users_files_ref tables, I'm just wondering how to manage the adding of file records for this scenario?
Thanks for any help provided, it's much appreciated.
-Wes
Use $this->db->insert_id() to get the id number of the row you just inserted. Further documentation here: http://codeigniter.com/user_guide/database/helpers.html
You're basically describing how it normally is done, with one important adjustment: how you retrieve the file_id of the file to be able to add it to users_files_ref.
Normally in a database environment you have many clients connecting at the same time, doing updates simultaneously. In such an environment you can't just get the file_id of the last file added - it might be someone elses file added in between your DB calls. You have to use functionality of the database to get the ID generated (e.g. SELECT ##IDENTITY on MSSQL) or generate the IDs in the application code somehow.
I think what you need is just this:
----primary key-----
users_files_ref -> | user_id, file_id |
How you get the the file_id is dependent on the code you're implementing. Your reasoning is correct. You already have the user_id and just need to get the file_id. With these values you can add a new row to user_files_ref.
When I need to do this I usually have a stored procedure with the help of a sequence that inserts the file and returns the sequence NEXTVAL as the output. This might be a way of implementing such cenario.
This is the code for an Oracle based stored procedure:
CREATE OR REPLACE PROCEDURE SP_IMPORT_FILE(FILE IN FILE.FILE%TYPE,
FILE_ID OUT NUMBER)
IS
BEGIN
SELECT SEQ_FILE.NEXTVAL INTO FILE_ID from DUAL;
INSERT INTO FILE (FILE_ID, FILE) VALUES (FILE_ID, FILE);
END SP_IMPORT_FILE;

Resources