symfony init-project test
w Database.yml ustawiłem parametry BD.
nastepnie:
symfony propel:build-all
symfony init-app frontend
symfony propel:generate-module frontend article DemoArticle
dodaje:
CODE
// lib/form/DemoArticleForm.class.php
$this->widgetSchema['author_id']->setOption('renderer_class', 'sfWidgetFormPropelJQueryAutocompleter');
$this->widgetSchema['author_id']->setOption('renderer_options', array(
'model' => 'DemoAuthor',
'url' => $this->getOption('url'),
));
public function executeNew(sfWebRequest $request)
{
$this->form = new DemoArticleForm($article, array('url' => $this->getController()->genUrl('article/ajax')));
}
// apps/frontend/modules/article/actions/actions.class.php
public function executeAjax($request)
{
$this->getResponse()->setContentType('application/json');
$authors = DemoAuthorPeer::retrieveForSelect($request->getParameter('q'), $request->getParameter('limit'));
return $this->renderText(json_encode($authors));
}
class DemoAuthorPeer extends BaseDemoAuthorPeer
{
static public function retrieveForSelect($q, $limit)
{
$criteria = new Criteria();
$criteria->add(DemoAuthorPeer::NAME, '%'.$q.'%', Criteria::LIKE);
$criteria->addAscendingOrderByColumn(DemoAuthorPeer::NAME);
$criteria->setLimit($limit);
$authors = array();
foreach (DemoAuthorPeer::doSelect($criteria) as $author)
{
$authors[$author->getId()] = (string) $author;
}
return $authors;
}
}
// apps/frontend/modules/article/templates/_form.php
<?php use_javascript('/sfFormExtraPlugin/js/jquery.autocompleter.js') ?>
<?php use_stylesheet('/sfFormExtraPlugin/css/jquery.autocompleter.css') ?>
Dostaje błąd:
Notice: Undefined variable: article in /home/sfprojects/test/apps/frontend/modules/article/actions/actions.class.php on line 20
wiec w lini:
$this->form = new DemoArticleForm
($article, array('url' => $this->getController()->genUrl('article/ajax')));
daje:
$this->form = new DemoArticleForm
(new DemoArticle
(), array('url' => $this->getController()->genUrl('article/ajax')));
dostaje blad:
Fatal error: Class 'sfWidgetFormPropelJQueryAutocompleter' not found in /usr/share/php/symfony/widget/sfWidgetFormChoice.class.php on line 126
Bo zapomniełem zainstalować pluginu
wiec:
symfony plugin:install sfFormExtraPlugin
i
symfony cache:clear
nastepnie blad:
500 | Internal Server Error | RuntimeException
Class "DemoAuthor" must implement a "__toString" method to be rendered in a "sfWidgetFormPropelJQueryAutocompleter" widget
wiec dodaje metody __toString
CODE
class DemoAuthor extends BaseDemoAuthor
{
public function __toString()
{
return $this->getName();
}
}
class DemoCategory extends BaseDemoCategory
{
public function __toString()
{
return $this->getName();
}
}
class DemoTag extends BaseDemoTag
{
public function __toString()
{
return $this->getName();
}
}
w bazie w demo_author dodaje 2 wpisy:
xtest 1
xtest 2
na końcu dałem jeszcze:
symfony plugin:publish-assets
i nadal nie działa
w article/new mam
CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="shortcut icon" href="/favicon.ico" />
<script type="text/javascript" src="/sfFormExtraPlugin/js/jquery.autocompleter.js"></script>
<link rel="stylesheet" type="text/css" media="screen" href="/css/main.css" />
<link rel="stylesheet" type="text/css" media="screen" href="/sfFormExtraPlugin/css/jquery.autocompleter.css" />
</head>
<body>
<h1>New Article</h1>
<form action="/article/create" method="post" >
<table>
<tfoot>
<tr>
<td colspan="2">
<input type="hidden" name="demo_article[id]" id="demo_article_id" /> <a href="/article">Cancel</a>
<input type="submit" value="Save" />
</td>
</tr>
</tfoot>
<tbody>
<tr>
<th><label for="demo_article_author_id">Author id</label></th>
<td>
<input type="hidden" name="demo_article[author_id]" id="demo_article_author_id" /><input type="text" name="autocomplete_demo_article[author_id]" value="" id="autocomplete_demo_article_author_id" /><script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#autocomplete_demo_article_author_id")
.autocomplete('/article/ajax', jQuery.extend({}, {
dataType: 'json',
parse: function(data) {
var parsed = [];
for (key in data) {
parsed[parsed.length] = { data: [ data[key], key ], value: data[key], result: data[key] };
}
return parsed;
}
}, { }))
.result(function(event, data) { jQuery("#demo_article_author_id").val(data[1]); });
});
</script> </td>
</tr>
<tr>
<th><label for="demo_article_status">Status</label></th>
<td>
......CUT
article/ajax daje mi plik z danymi z bazy czyli:
{"2":"xtest 1","1":"xtest 2"}
tak to wygląda...