var customer = new app.models.Customer(); customer.name = 'Vladimir'; customer.save();
db.createCommand('INSERT INTO `customer` (`name`) VALUES (:name)', { ':name': 'Vladimir', }).execute();
var Jii = require('jii'); /** * @class app.models.Customer * @extends Jii.sql.ActiveRecord */ Jii.defineClass('app.models.Customer', /** @lends app.models.Customer.prototype */{ __extends: Jii.sql.ActiveRecord, __static: /** @lends app.models.Customer */{ tableName: function() { return 'customer'; } } });
return { components: { db: { className: 'Jii.sql.mysql.Connection', host: '127.0.0.1', database: 'testdb', username: 'demo', password: 'demo', charset: 'utf8', } } };
/** * @class app.models.Customer * @extends Jii.sql.ActiveRecord */ Jii.defineClass('app.models.Customer', /** @lends app.models.Customer.prototype */{ __extends: Jii.sql.ActiveRecord, __static: /** @lends app.models.Customer */{ STATUS_INACTIVE: 0, STATUS_ACTIVE: 1, // ... getDb: function() { // use the "db2" application component return Jii.app.db2; } } });
// ID 123 // SELECT * FROM `customer` WHERE `id` = 123 app.models.Customer.find() .where({id: 123}) .one() .then(function(customer) { // ... }); // , ID // SELECT * FROM `customer` WHERE `status` = 1 ORDER BY `id` app.models.Customer.find() .where({status: app.models.Customer.STATUS_ACTIVE}) .orderBy('id') .all() .then(function(customers) { // ... }); // // SELECT COUNT(*) FROM `customer` WHERE `status` = 1 app.models.Customer.find() .where({status': app.models.Customer.STATUS_ACTIVE}) .count() .then(function(count) { // ... }); // , ID // SELECT * FROM `customer` app.models.Customer.find() .indexBy('id') .all() .then(function(customers) { // ... });
// ID 123 // SELECT * FROM `customer` WHERE `id` = 123 app.models.Customer .findOne(123) .then(function(customer) { // ... }); // ID 100, 101, 123 124 // SELECT * FROM `customer` WHERE `id` IN (100, 101, 123, 124) app.models.Customer .findAll([100, 101, 123, 124]) .then(function(customers) { // ... }); // ID 123 // SELECT * FROM `customer` WHERE `id` = 123 AND `status` = 1 app.models.Customer .findOne({ id: 123, status: app.models.Customer.STATUS_ACTIVE }) .then(function(customer) { // ... }); // // SELECT * FROM `customer` WHERE `status` = 0 app.models.Customer .findAll({ status: app.models.Customer.STATUS_INACTIVE }) .then(function(customers) { // ... });
Note: Neither Jii.sql.ActiveRecord.findOne () nor Jii.sql.ActiveQuery.one () will add `LIMIT 1` to the SQL expression. If your query can really return a lot of data, then you need to call `limit (1)` to set the limit, for example, ʻapp.models.Customer.find (). Limit (1) .one () `.
// var sql = 'SELECT * FROM customer WHERE status=:status'; app.models.Customer .findBySql(sql, {':status': app.models.Customer.STATUS_INACTIVE}) .all() .then(function(customers) { // ... });
// "id" "email" "customer" app.models.Customer .findOne(123) .then(function(customer) { var id = customer.get('id'); var email = customer.get('email'); });
In fact, in JavaScript you get an array filled with objects. Therefore, it would be more correct to call the method asObject () , and there is such a method (synonym). But to save the API Yii 2, the asArray () method was left.
// , // app.models.Customer.find() .asArray() // alias is asObject() .all() .then(function(customers) { // ... });
// var customer = new app.models.Customer(); customer.set('name', 'James'); customer.set('email', 'james@example.com'); customer.save().then(function(success) { return app.models.Customer.findOne(123); }).then(function(customer) { // customer.set('email', 'james@newexample.com'); return customer.save(); }).then(function(success) { // ... });
var values = { name: 'James', email: 'james@example.com' }; var customer = new app.models.Customer(); customer.setAttributes(values); customer.save();
var customer = new app.models.Customer(); customer.loadDefaultValues(); // customer.get('xyz') `xyz` - `xyz`.
// UPDATE `customer` SET `status` = 1 WHERE `email` LIKE `%@example.com%` app.models.Customer.updateAll({status: app.models.Customer.STATUS_ACTIVE}, {'like', 'email', '@example.com'});
app.models.Customer .findOne(123) .then(function(customer) { customer.delete(); });
app.models.Customer.deleteAll({status: app.models.Customer.STATUS_INACTIVE});
/** * @class app.models.Customer * @extends Jii.sql.ActiveRecord */ Jii.defineClass('app.models.Customer', /** @lends app.models.Customer.prototype */{ // ... getOrders: function() { return this.hasMany(app.models.Order.className(), {customer_id: 'id'}); } }); /** * @class app.models.Order * @extends Jii.sql.ActiveRecord */ Jii.defineClass('app.models.Order', /** @lends app.models.Order.prototype */{ // ... getCustomer: function() { return this.hasOne(app.models.Customer.className(), {id: 'customer_id'}); } });
// SELECT * FROM `customer` WHERE `id` = 123 app.models.Customer .findOne(123) .then(function(customer) { // SELECT * FROM `order` WHERE `customer_id` = 123 return customer.load('orders'); }) .then(function(orders) { // orders - `app.models.Order` });
/** * @class app.models.Order * @extends Jii.sql.ActiveRecord */ Jii.defineClass('app.models.Order', /** @lends app.models.Order.prototype */{ // ... getItems: function() { return this.hasMany(app.models.Item.className(), {id: 'item_id'}) .viaTable('order_item', {order_id: 'id'}); } });
/** * @class app.models.Order * @extends Jii.sql.ActiveRecord */ Jii.defineClass('app.models.Order', /** @lends app.models.Order.prototype */{ // ... getOrderItems: function() { return this.hasMany(app.models.OrderItem.className(), {order_id: 'id'}); }, getItems: function() { return this.hasMany(app.models.Item.className(), {id: 'item_id'}) .via('orderItems'); } });
// SELECT * FROM `order` WHERE `id` = 100 app.models.Order .findOne(100) .then(function(order) { // SELECT * FROM `order_item` WHERE `order_id` = 100 // SELECT * FROM `item` WHERE `item_id` IN (...) return order.load('items'); }) .then(function(items) { // items - `app.models.Item` });
// SELECT * FROM `customer` WHERE `id` = 123 app.models.Customer .findOne(123) .then(function(customer) { // SELECT * FROM `order` WHERE `customer_id` = 123 customer.load('orders').then(function(orders) { // SQL return customer.load('orders'); }).then(function(orders2) { // , <i>get()</i>. var orders3 = customer.get('orders'); }); });
// SELECT * FROM `customer` LIMIT 100 app.models.Customer.find() .limit(100) .all() .then(function(customers) { return Promise.all(customers.map(function(customer) { // SELECT * FROM `order` WHERE `customer_id` = ... return customer.load('orders'); })); }).then(function(result) { var firstOrder = result[0][0]; // ... });
// SELECT * FROM `customer` LIMIT 100; // SELECT * FROM `orders` WHERE `customer_id` IN (...) app.models.Customer.find() .with('orders') .limit(100) .all() .then(function(customers) { customers.forEach(function(customer) { // SQL var orders = customer.get('orders'); }); });
// "orders" "country" app.models.Customer.find() .with('orders', 'country') .all() .then(function(customers) { // ... }); // app.models.Customer.find() .with(['orders', 'country']) .all() .then(function(customers) { // SQL var orders = customers[0].get('orders'); var country = customers[0].get('country'); }); // "orders" "orders.items" app.models.Customer.find() .with('orders.items') .all() .then(function(customers) { // // SQL var items = customers[0].get('orders')[0].get('items'); });
// // SELECT * FROM `customer` // SELECT * FROM `country` WHERE `id` IN (...) // SELECT * FROM `order` WHERE `customer_id` IN (...) AND `status` = 1 app.models.Customer.find() .with({ country: 'country', orders: function (query) { query.andWhere({'status': app.models.Order.STATUS_ACTIVE}); } }) .all() .then(function(customers) { // ... });
/** * @class app.models.Customer * @extends Jii.sql.ActiveRecord */ Jii.defineClass('app.models.Customer', /** @lends app.models.Customer.prototype */{ // ... getOrders: function() { return this.hasMany(app.models.Order.className(), {customer_id: 'id'}); } }); /** * @class app.models.Order * @extends Jii.sql.ActiveRecord */ Jii.defineClass('app.models.Order', /** @lends app.models.Order.prototype */{ // ... getCustomer: function() { return this.hasOne(app.models.Customer.className(), {id: 'customer_id'}); } });
// SELECT * FROM `customer` WHERE `id` = 123 app.models.Customer .findOne(123) .then(function(customer) { // SELECT * FROM `order` WHERE `customer_id` = 123 return customer.load('orders'); }).then(function(orders) { var order = orders[0]; // SELECT * FROM `customer` WHERE `id` = 123 return order.load('customer'); }).then(function(customer2) { // "not the same" console.log(customer2 === customer ? 'same' : 'not the same'); });
/** * @class app.models.Customer * @extends Jii.sql.ActiveRecord */ Jii.defineClass('app.models.Customer', /** @lends app.models.Customer.prototype */{ // ... getOrders: function() { return this.hasMany(app.models.Order.className(), {customer_id: 'id'}).inverseOf('customer'); } });
// SELECT * FROM `customer` WHERE `id` = 123 app.models.Customer .findOne(123) .then(function(customer) { // SELECT * FROM `order` WHERE `customer_id` = 123 return customer.load('orders'); }).then(function(orders) { var order = orders[0]; // SELECT * FROM `customer` WHERE `id` = 123 return order.load('customer'); }).then(function(customer2) { // "same" console.log(customer2 === customer ? 'same' : 'not the same'); });
: Many-Many, (Junction Table).
app.models.Customer .findOne(123) .then(function(customer) { var order = new app.models.Order(); order.subtotal = 100; // ... // , "customer" `app.models.Order` . order.customer_id = customer.id; order.save(); });
app.models.Customer .findOne(123) .then(function(customer) { var order = new app.models.Order(); order.subtotal = 100; // ... order.link('customer', customer); });
: Active Record.
order.link('items', item);
app.models.Customer.find() .with('orders') .all() .then(function(customer) { customer.unlink('orders', customer.get('orders')[0]); });
Source: https://habr.com/ru/post/260569/
All Articles