Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
oil Scaffolding with Foreign Keys
  • Hello. First, let me say that FuelPHP looks like a future PHP framework winner (worked so far with CI, CakePHP and Yii).
    Just looking forward to see what's next on your agenda. I do have some questions/issues about oil scaffold generation (if I should post this in ORM section, please excuse me). Let's say we have 2 tables, linked with a Foreign Key (tables were added in database with oil: g scaffold city ..., g scaffold user ..., the FK was added directly into DB).
    CREATE TABLE `cities` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(255) NOT NULL,
      `created_at` int(11) NOT NULL,
      `updated_at` int(11) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB;
    
    and
    CREATE TABLE `users` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(255) NOT NULL,
      `city_id` int(11) NOT NULL,
      `created_at` int(11) NOT NULL,
      `updated_at` int(11) NOT NULL,
      PRIMARY KEY (`id`),
      CONSTRAINT `users_ibfk_0` FOREIGN KEY (`city_id`) REFERENCES `cities` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
    ) ENGINE=InnoDB;
    

    My issues:
    1. How can I specify to oil (on generate) that city_id should be a Foreign Key ?
    2. For tables with FK, oil should generate controllers with _has_one / _belongs_to, according to foreign keys.
    3. When views are created for 'users', the view should display, by default, City Name (from cities) instead of city_id.
    4. When _form is created for editing 'users', the form should generate a Form::select for city_id, with values taken from cities (Name).
    5. Added feature: oil should be able to generate models/controllers/views from existing database (see CRUDE: http://fuelphp.com/forums/topics/view/3959) I have spend 6 hours today, searching in forum and documentation for some answers to my issues, but no lack so far. Regards,
    /Sorin
  • I think much of this is a fundamental misunderstanding of how it works. The Scaffolding happens in essentially the same way as code generation in Rails. Step 1: Define your data through simple syntax in the command line
    Step 2: BASIC code is generated, which can be edited later
    Step 3: Migration is run to add the database Anything else and you are fundamentally altering how all this logic works. Having a system that builds code from a database is backwards to how this is all designed.
  • but is it possible to have related models in scaffolding/generating models? if so, how?
  • Trying to develop scaffolding for all possible kinds of relationships would be a gigantic task - and it would be very difficult to come up with a standard interface that would be suitable for everybody. Think of a simple one-to-many relationship. for instance, and all the different ways that it can be displayed. It is possible, nevertheless, to write your own tasks for Oil, so perhaps it would be a matter of writing your own custom scaffolding command. Perhaps you can start by looking at the existing scaffolding code, and extend it from there.
  • Why would you need to? Oil just generated the majority of your code then you tweak it however you like afterwards. People focus too much on trying to make simple things do EVERYTHING and then it ends up being a confusing mess.
  • Hello Phil and thanks for the answer. Let me disagree with you on this issue. 1. Seems to be normal to me as a developer to be able to define data and relations (at least foreign keys) at once.
    Using oil to define just data, then using a database admin tool to define FKs and go back to my controller to define _has_one / _has_many, could be a "longer way" for a PHP framework (btw, maybe I missed something on oil generation). 2.
    Anything else and you are fundamentally altering how all this logic works.
    Having a system that builds code from a database is backwards to how this is all designed.

    I have seen on FuelPHP 1.1 road map that there is an Admin generator, something in Django style. Can't see any reason to generate the admin part directly with oil. Having this in FuelPHP will be a good start for any web developer wants in any web application, and this this will reduce the application start up. Anyway, that's why all developers are using frameworks instead of starting a new app just with good ol' PHP lying beneath. Just my 2 cents. Regards,
    /Sorin

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion