简体   繁体   English

使用现有数据库设置CakePHP

[英]Setup CakePHP with an existing Database

I have a MySQL server database currently setup which has a few simple tables to track orders, such as tblOrders, tblUsers, tblProducts. 我目前正在设置一个MySQL服务器数据库,该数据库具有一些用于跟踪订单的简单表,例如tblOrders,tblUsers,tblProducts。

Although I have a website working with it fine now, I'd like to setup CakePHP to act as the server side framework for handling interaction with the database rather than using hand written queries in my PHP pages. 尽管我现在有一个可以正常使用的网站,但我想将CakePHP设置为服务器端框架,以处理与数据库的交互,而不是在PHP页面中使用手写查询。

Is there a simple way to setup CakePHP with my existing Database/tables? 有没有一种简单的方法可以用我现有的数据库/表设置CakePHP?

If I understand correctly, I will have a main MyApplication class which Extends Controller, as well as Order, User, Product, (... other tables) classes which each extend the MyApplication class. 如果我理解正确,我将拥有一个扩展控制器的MyApplication主类,以及分别扩展MyApplication类的Order,User,Product和其他类。

It looks like the REST guide uses a method in the configuration file called Router::mapResources('recipes'); 看起来REST指南在配置文件中使用了一种称为Router::mapResources('recipes'); . Will this create the controllers for each table with the default methods to use for REST? 这会使用用于REST的默认方法为每个表创建控制器吗?

Ie, in the /app/config/routes.php configuration file: 即,在/app/config/routes.php配置文件中:

// /app/config/routes.php
Router::mapResources('tblOrders');
Router::mapResources('tblUsers');
Router::mapResources('tblProducts');

// /app/config/database.php
<?php
class DATABASE_CONFIG {
    public $default = array(
        'datasource'  => 'Database/myStoreDB',
        'persistent'  => false,
        'host'        => 'localhost',
        'login'       => 'admin_user',
        'password'    => 'c4k3roxx!',
        'database'    => 'main_db',
        'prefix'      => ''
    );
}

If you want Model, Controller and View code created for you, you're looking for baking . 如果要为您创建模型,控制器和视图代码,则需要进行烘烤

Cake expects your database tables to follow a naming convention . Cake希望您的数据库表遵循命名约定 I'm not sure how the bake will go because your tables don't match this convention. 我不确定烘烤会如何进行,因为您的餐桌与该约定不符。 You may need to create your model classes by hand and specify the useTable attribute on each class. 您可能需要手动创建模型类,并在每个类上指定useTable属性。 For example: 例如:

<?php
class Order extends AppModel {
    public $useTable = 'tblOrders';
}

Once this is done, I expect the baking of Controllers and Views should work as normal. 完成此操作后,我希望对Controllers和Views的烘焙应能正常进行。 Note that with mapResources you still need Controller code. 请注意,使用mapResources仍需要控制器代码。 That function just generates routes. 该功能只是生成路线。

If Cake is the only thing that will be touching this database, I recommend renaming tables and columns in line with the conventions it expects. 如果Cake是唯一要使用此数据库的东西,我建议按照其期望的约定重命名表和列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM