Fetch data for a select query in yii migrations - yii

I am creating a parent child table using yii migration where there is a foreign key on the child table. I tried using execute but that did not help me fetching the data needed. Is there a way I can query the parent table and insert the parent id on the child table as a foreign key? Something like fetch()/fetchAll() that is used inside phinx migrations.
$this->batchInsert('{{%parent_table}}',
['name'],
[
['P1'],
['P2'],
['P3'],
['P4']
]
);
$a = $this->execute("select * from parent_table where name = 'P1'");
// get the data from the table that will get me the id from the above query.
$this->batchInsert('{{%child_table}}',
[['name'],['parent_id']],
[
['C1','<parent id>'],
['C2','<parent id>'],
.
.
.
]
);

You need to use
$sql = <<<SQL
select * from parent_table where name = 'P1'
SQL;
$res = Yii::$app->db->createCommand($sql)->queryAll();
The $this->execute() is used to execute sql statements like update or delete instead
Edit
Better use $this->db instead of Yii::$app->db to make sure you are running on the same db, as mentioned in the comments

Related

Upsert query is not updating the the records in postgres

I am trying to Upsert into the target table from temp table but
My upsert query is not updating the records in postgresql. Although it is inserting but not updating.
Please find below query:
Insert into store
Select t.* from store_temp
where t.id not in (select a. Id
from store a) on conflict(id)
DO
Update
Set source = EXCLUDED.Source
Any helping hand would be really appreciated.
Your syntax looks correct, but I don't think you want the where clause. Instead:
Insert into store ( . . . )
select . . .
from store_temp t
on conflict (id) do update
set source = EXCLUDED.Source;
The . . . are for the column list. I recommend being explicit in inserts.
Then you need to be sure that id is declared as the primary key or at least has a unique constraint or index.

How to prevent delete query in SQL

I have created a database through Entity Framework Code First Approach and My application is ready and running live . The problem is that I did not turned "False" on Cascade Delete at the time of creating database.
Now if I delete any record from one table that is referenced with another table through foreign so all the record containing foreign key of deleted row is deleted from another table .
Practically demonstration :
Let say I have a Table called Passenger:
ID Name CategoryID
1 ABC 1
CategoryID here is a foreign key
Here is the category Table
ID Name
1 Gold
Let say I run my query on category table
delete from Category where ID = 1
Now all the record from my Passenger Table is deleted . I want to restrict it. Is it Possible through SQL now ?
I suppose
This is what you are looking for :
alter TRIGGER customers_del_prevent
ON dbo.customers
INSTEAD OF DELETE
AS
BEGIN
insert into dbo.log
values ('DELETE')
RAISERROR ('Deletions not allowed from this table (source = instead of)', 16, 1)
END
Hope this helps you. :)

TYPO3 Extbase query based on other table

I got to table where one related to the other
TABLE A
uid
name
TABLE B
uid
name
rel_table_a
Now I would like:
// find all table A that is related to by table B
public function findAllTableAThatIsRelatedToByTableB() {
$query = $this->createQuery();
// what should this line be?
//$query->matching($query->count(tableB.rel_table_a = uid));
return $query->execute();
}
Is that possible or will the relation need to be created in the TCA for Table A first.
You can use related tables in the queries, but first you need to set up your TCA and your models properly, otherwise extbase does not know how your tables are related.
As the query condition you could just check if the relation exists.
In your structure we can only do this query on the table B since your table A does not have a relation field, so in this example you will get all items from table B that have a table A element related.
$query->matching($query->greaterThan('rel_table_a.uid', 0));
If you have an 1:n relation from table A to table B, then you should use an inline field. This way you can do a query on the table A: https://docs.typo3.org/typo3cms/TCAReference/Reference/Columns/Inline/Index.html
PS: you can not use $query->count inside a $query->matching

web2py - programmatically insert on one-to-many db

Goodmorning,
I've a DB with three tables :
db.define_table('person',
Field('name', length=100),
Field('dob', length=10),
Field('address', 'text', length=255),
Field('countryname', length=3),
Field('statename', length=100),
)
db.define_table('opslist',
Field('opid', length=10),
Field('dop', length=10),
Field('type_operation', length=25),
)
db.define_table('cardlist',
Field('opid', db.opslist),
Field('name', db.person),
Field('countryname', length=3, required=True),
Field('statename', length=100, required=True),
Field('zip', length=5, required=False),
)
I would insert by code, means not using a sqlform or a form, some data into ...
e.g.: If i have all the values insertable into cardlist table the operation must give me the ability to fill automatically the related record (creating new if not present) into the other tables.
This because it's a relational db with foreign keys ...
Is this possible with web2py using the DAL ? or do i have to write my raw sql command ?
Thank You All
Just use the insert method. It returns the ID of the newly created record, which you can use to insert into the reference fields:
person = db.person.insert(name=..., dob=..., ...)
opslist = db.opslist.insert(opid=..., dop=..., ...)
db.cardlist.insert(opid=opslist, name=person, ...)

How do you modify existing records using ScalaQuery?

By modify I mean counterparts of SQL UPDATE and DELETE.
In both cases I have an object-record and I would like to delete it in the database. The table has always primary key, and it is set in my object-record.
Please note that I don't have query or other source which "created" that object-record, all I have is it and the table. So in general it looks like this:
fetch the Record from Table
...
// forget how I get the Record
...
Record.person_name = "joe"
? update Record ?
How to do it?
I define records and tables as below:
case class Topic(var id : Long,
var sectionId : Int,
...
object TopicTable extends Table[Topic]("Topic") {
def id = column[Long]("top_Id", O.PrimaryKey)
def sectionId = column[Int]("sect_Id")
...
It seems there are no direct methods, so you have to create explicitly a recordset in order to modify (for comparison -- I know SQ is not ORM -- in EF you fetch records, modify them and at this point your data context "knows" they were modify, so all you have to do is submit changes).
So first you create RS as you like:
val rs = for (rec <- MyTable if rec.id===10) yield rec;
and the delete records:
rs.mutate(rec => rec.delete())
for update:
rs.update(new MyRecord(...))
or (gossip is, it is faster ;-) )
rs.mutate(rec => rec.row = new MyRecord(...))
Please note I am complete newbie with SQ so I might just misinformed you. I works for me though.
Now, the only missing part is adding some nice wrappers, so delete and update could be done directly per record.