Getting Started with Database

I am new to Yii and trying to port an existing PHP application to use Yii. But I am having trouble getting started.

There are three initial issues that are puzzling me.

First - how does Yii create the database structure. I see a file in the data directory called schema.sqlite.sql which is used to setup the database. In my existing application I have some code like this




if(!file_exists(AIR_HOCKEY_DATABASE.AIR_HOCKEY_VARIANT.'.db')) {

	$db = new PDO('sqlite:'.AIR_HOCKEY_DATABASE.AIR_HOCKEY_VARIANT.'.db');

    $db->exec(file_get_contents('./database.sql'));

} else {

	$db = new PDO('sqlite:'.AIR_HOCKEY_DATABASE.AIR_HOCKEY_VARIANT.'.db');

}



and then later, allowing for potential updates to the database structure




$result = $db->query("SELECT count(*) FROM sqlite_master WHERE name = 'config' ;");

if(!($result && ($present = $result->fetchColumn()) && $present == '1')) {

	//NO CONFIG TABLE - so database must be at version 1 - update to version 2

	$result->closeCursor();

    $db->exec(file_get_contents('./update1.sql'));	    

} else {

	$result->closeCursor();

}



Where in the yii framework should I put this stuff?

The second issue I have is that I need to make my menu dynamic, reading a database query. I need to read a the database and create a list of menu items (and the second level) from a table in the database. In the default application built by yiic the menu is in the layout. with a call to




$this->widget('zii.widgets.CBreadcrumbs' ...



How am I supposed to setup to correct data for it to draw a dynamic menu?

The last issue I have is that the default page (ie the site/index action) should comprise several sections, which should ideally be a separate model and view.

But I don’t understand how I should do that. If it was just one view then site controller does




$this->render('index');



but this causes the layout to surround it which is fine.

But if I did




$this->render('part1');

$this->render('part2');



Then I would get two sets of layout. But if I did




$this->renderPartial('part1');

$this->renderPartial('part2');



I presume I have no layout. So what am I supposed to do.

I got the wrong bit - I meant this




	<div id="mainmenu">

		<?php $this->widget('zii.widgets.CMenu',array(

			'items'=>array(

				array('label'=>'Forum', 'url'=>'http://mb.home/forum'),

				array('label'=>'User Page', 'url'=>array('/site/index', 'view'=>'about')),

				array('label'=>'Admin', 'url'=>array('/admin/index'),'visible'=>Yii::app()->user->isAdmin),

			),

		)); ?>

	</div><!-- mainmenu -->



Yii doesn’t create the database schema for you. The examples you found in the data folder are simply that (examples).

Generally you create the database and are able to use Gii to quickly generate models and CRUD functionality which are then expanded upon to create your application.

Of course, Yii can do what you’re asking, but this is custom functionality that you need to program yourself. Even the example you gave was created the same way, ie. the database schema was already created in ‘database.sql’.

I’m sorry but I think you really need to learn a little more about Yii (and maybe php/MySql in general) first, unless someone is willing to write your application for you.