Laravel: Retrieve the one-and-only record from database - singleton

How can I retrieve the one-and-only record from the database w/o Model::all();?
I do not know it's id either so I can't use Model::find($id);.

You can use the first() method to get the first record from the database matching additional conditions if any are included:
$record = Model::first();
This will return an instance of Model, or null if there are no records present.

Related

CloudKit, NSPredicate to return a count or determine if any records exists , in a private container?

I've been researching how to determine is any RecordType records exists in a Private Container, perhaps from a previous app installation or from another device in the users iCloud account.
I see that you can not perform an NSPredicate count of records.
However I can't find an alternative to find if any records exists?
There is no way to get a count.
If you wish to determine if there are any records for a given record type, perform a CKQueryOperation for the given record type. Set the query's predicate to [NSPredicate predicateWithValue:YES] and set the operation's resultLimit to 1.
Then check the results. You'll either get one row back if there are any records or you'll get no rows back (or possible an error, see what happens).

Duplicate a record and its references in web2py

In my web2py application I have a requirement to duplicate a record and all its references.
For example
one user has a product (sponserid is the user). and this product has so many features stored in other tables (reference to product id).
And my requirement is if an another user is copying this product, the a new record will generate in the product table with new productid and new sponserid. And all the reference table records will also duplicate with the new product id. Effectively a duplicate entry is creating in all the tables only change is product id and sponserid.
The product table fields will change. So I have to write a dynamic query.
If I can write a code like below
product = db(db.tbl_product.id==productid).select(db.tbl_product.ALL).first()
newproduct = db.tbl_product.insert(sponserid=newsponserid)
for field,value in product.iteritems():
if field!='sponserid':
db(db.tbl_product.id==newproduct).update(field=value)
But I cannot refer a field name like this in the update function.
Also I would like to know if there is any other better logic to achieve this requirement.
I would greatly appreciate any suggestions.
For the specific problem of using the .update() method when the field name is stored in a variable, you can do:
db(db.tbl_product.id==newproduct).update(**{field: value})
But an easier approach altogether would be something like this:
product = db(db.tbl_product.id==productid).select(db.tbl_product.ALL).first()
product.update(sponserid=newsponserid)
db.tbl_product.insert(**db.tbl_product._filter_fields(product))
The .update() method applied to the Row object updates only the Row object, not the original record in the db. The ._filter_fields() method of the table takes a record (Row, Storage, or plain dict) and returns a dict including only the fields that belong to the table (it also filters out the id field, which the db will auto-generate).

Delete() and Deleteall() in Hibernate

Consider the below two methods to delete a Set of Employees with the name "John" .
List<Employee> list = new ArrayList<Employee>();
String query= " from Employee emp where emp.name = 'John'";
list=getHibernateTemplate().find(query);
First Method:
getHibernateTemplate().deleteAll(list);
Second Method:
Iterator<BulkChangeRequest> itList = list.iterator();
while(itList.hasNext()) {
Employee emp = itList.next();
getHibernateTemplate().delete(emp);
}
Do they differ significantly in terms of performance? Are both of them essentially the same i.e. does the deleteAll method delete row one by one?
Also wouldn't it be better to do it in SQL using the following query?
" delete from Employee where name = 'John'"
Yes. It would be better to use SQL or HQL to delete all employee records named 'John' rather then the first option. Simply because in first option, you have to load all employee record from db to delete. That's obviously an extra task.
I think, for delete() and deleteAll(), there will be no difference except in delete(), it will create new session for every call; whereas in deleteAll(), all objects will be deleted in one session. But the number of queries will be the same.
I suggest you read this section, one shot delete:
https://docs.jboss.org/hibernate/orm/3.5/reference/en/html/performance.html#performance-collections-oneshotdelete
Deleting collection elements one by one can sometimes be extremely
inefficient. Hibernate knows not to do that in the case of an
newly-empty collection (if you called list.clear(), for example). In
this case, Hibernate will issue a single DELETE.

how does upsert with external id work?

How does upsert work in the Salesforce API?
I believe that it checks it checks if there is a record with unique id. In the case that it is available, then it updates the record otherwise the record is created.
Is this correct?
I am receiving the following error
Upsert failed. First exception on row 1; first error: DUPLICATE_EXTERNAL_ID, Asset Tag: more than one record found for external id field: [a11M0000000CwJqIAK, a11M0000000CwJvIAK]: [Asset_Tag__c]
I have a list with items, and there are no duplicate Asset_Tag values.
system.debug('LstItem Asset_Tag__c'+LstItem );
upsert LstItem Asset_Tag__c;
From the debug log
LstItem Asset_Tag__c(Item_c__c:{Scanned_By__c=005M0000000IlxyIAC, Asset_Tag__c=12149, Status__c=Active, Scan_Location__c=001M0000008GzJXIA0, Last_Scan_Date__c=2011-12-17 06:08:47}, Item_c__c:{Scanned_By__c=005M0000000IlxyIAC, Asset_Tag__c=23157, Status__c=Active, Scan_Location__c=001M0000008GzJXIA0, Last_Scan_Date__c=2011-12-17 08:26:14})
What can I do to resolve this issue?
The error message indicates that, based on the External Id value you provided, there were two matching records. In this case, the system does not know which one it should update, so it fails.
If you take a look at /a11M0000000CwJqIAK and /a11M0000000CwJvIAK, they will have the same value in the external ID field. You may want to consider de-duplicating the records for this object.

tests for data access (sql queries) functionality

I want to know the best way of testing data access functionality.
I know that it is possible to create mocks to change data layer objects that is using to test business logic.
However is it possible to test if sql queries to database are correct.
Scenario: A method must return employees which were applied for work last month.
I can return list of object and check if each employee's startDate property is correct (last month).
So if it returns 3 employees and they have correct startDate value but there are more two employee in database which aren't returned. How to write test for that case? :)
Thanks in advance.
You set up the test DB so you shold know what data is in it. If you expect 5 employees to be returned from the query and you get only 3, you know there is an error.
You can test the query with different setups: empty table, only new employees, only old employees, a mix of the two (with special care to the borderline cases), etc.
I don't think you need to check the two other employees in the database which aren't returned.
The key is, when setting up your test data, you would want to ensure you have enough records that don't match the criteria (in addition to the records that do match), then you run the fetch and make sure you get back the correct number of records that do match the criteria.
Preparing the test data in this manner ensures that your method is returning the expected results.