如果没有Mage类,开发人员如何实例化模型或magento-singleton对象?该
Mage :: getModel和
Mage :: getSingleton方法已经退役,并在他们的地方Magento的有一个新的“对象管理器”的对象。
现在,此对象管理器是一个PHP单例,您可以通过以下调用进行抓取。
$ object_manager = MagentoCoreModelObjectManager :: getInstance();
Magento正在迈向一种体系结构,该对象将在适当的位置作为_objectManager属性自动可用。
$ this-> _ objectManager
protected $_resource;
public function __construct(
...
\Magento\Framework\App\ResourceConnection $resource
...
) {
$this->_resource = $resource;
}
$connection = $this->_resource->getConnection();
或者
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('employee'); //gives table name with prefix
从表中选择数据
$sql = "Select * FROM " . $tableName;
$result = $connection->fetchAll($sql); // gives associated array, table fields as key in array.
从表中删除数据
$sql = "Delete FROM " . $tableName." Where emp_id = 10";
$connection->query($sql);
将数据插入表
$sql = "Insert Into " . $tableName . " (emp_id, emp_name, emp_code, emp_salary) Values ('','XYZ','ABD20','50000')";
$connection->query($sql);
将数据更新到表中
$sql = "Update " . $tableName . "Set emp_salary = 20000 where emp_id = 12";
$connection->query($sql);