Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Migrating a Site to Silverstripe /

What you need to know when migrating your existing site to SilverStripe.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

integrate external PHP code


Go to End
Reply


15 Posts   6018 Views

Avatar
UncleCheese

Forum Moderator, 4092 Posts

24 March 2009 at 2:33am

So do this:

function StaffSearchForm()
{
return new Form(
$this,
"StaffSearchForm",
new FieldSet(new TextField('FirstName','First Name'), new TextField('LastName','Last Name')),
new FieldSet(new FormAction('doStaffSearch','Search'))
);
}

function doStaffSearch($data, $form)
{
$results = DataObject::get("SiteTree","ClassName IN('FacultyPage','StudentPage','StaffPage'), lastname LIKE '%".$data['FirstName']."%' AND firstname LIKE '%".$data['LastName']."%'");
return $this->customise(array('Results' => $results))->renderWith(array('StaffSearch_results','Page'));
}

You really should be using MATCH and AGAINST in place of like because it is built for searching. If someone misspells a teacher's name by one letter, your query will return null. the MATCH/AGAINST functions are built to tolerate user ambiguity and order your results by relevance.

If you're getting a white screen, you need to turn up your error reporting in PHP and find out what's going on. Probably a syntax error somewhere.

Avatar
Victor

Community Member, 128 Posts

24 March 2009 at 7:58am

It looks like error if search returns empty is due to collision with SiteSearchForm of blackcandy. Commenting it out solves the problem. How to avoid this collision?

Error message:

Trace

Page_Controller->results()
call_user_func_array(Array,Array)
Line 551 of ViewableData.php
ViewableData->cachedCall(Results,,)
Line 1003 of ViewableData.php
ViewableData_Customised->cachedCall(Results,,)
Line 592 of ViewableData.php
ViewableData->hasValue(Results)
Line 11 of .cache.Applications.MAMP.htdocs.SilverStripe.themes.blackcandy.templates.Layout.FacultySearch_results.ss
include(/private/var/tmp/silverstripe-cache-Applications-MAMP-htdocs-SilverStripe/.cache.Applications.MAMP.htdocs.SilverStripe.themes.blackcandy.templates.Layout.FacultySearch_results.ss)
Line 354 of SSViewer.php
SSViewer->process(ViewableData_Customised)
Line 346 of SSViewer.php
SSViewer->process(ViewableData_Customised)
Line 774 of ViewableData.php
ViewableData->renderWith(Array)
Line 28 of FacultyHolder.php
FacultyHolder_Controller->doFacultySearch(Array,Form,HTTPRequest)
Line 228 of Form.php
Form->httpSubmission(HTTPRequest)
Line 107 of RequestHandler.php
RequestHandler->handleRequest(HTTPRequest)
Line 121 of RequestHandler.php
RequestHandler->handleRequest(HTTPRequest)
Line 122 of Controller.php
Controller->handleRequest(HTTPRequest)
Line 28 of ModelAsController.php
ModelAsController->handleRequest(HTTPRequest)
Line 277 of Director.php
Director::handleRequest(HTTPRequest,Session)
Line 121 of Director.php
Director::direct(/faculty/FacultySearchForm)
Line 115 of main.php
****
Thanks! Victor

Avatar
UncleCheese

Forum Moderator, 4092 Posts

24 March 2009 at 8:10am

Try changing the name of the template variable so it doesn't collide.

function doStaffSearch($data, $form)
{
$results = DataObject::get("SiteTree","ClassName IN('FacultyPage','StudentPage','StaffPage'), lastname LIKE '%".$data['FirstName']."%' AND firstname LIKE '%".$data['LastName']."%'");
return $this->customise(array('StaffSearchResults' => $results))->renderWith(array('StaffSearch_results','Page'));
}

<% if StaffSearchResults %>
<% control StaffSearchResults %>

etc...

Avatar
Victor

Community Member, 128 Posts

24 March 2009 at 8:23am

Thanks! It fixes the original form. Had no chance to work with more advanced form

Victor

Avatar
Victor

Community Member, 128 Posts

24 March 2009 at 11:55pm

Thanks again. I am making progress but not completely.

1) I tried MATCH AGAINST. SilverStripe returned an error but MySQL query via phpMyAdmin returned

"Can't find FULLTEXT index matching the column list".

So, this closes the opportunity unless there is a way to force making such index

2) Currently I use only StaffPage and this works:

function doStaffSearch($data, $form)
{
$results = DataObject::get("StaffPage","lastname LIKE '%".$data['LastName']."%' AND firstname LIKE '%".$data['FirstName']."%'");
return $this->customise(array('StaffSearchResults' => $results))->renderWith(array('StaffSearch_results','Page'));
}


but this does not:

function doStaffSearch($data, $form)
{
$results = DataObject::get("SiteTree","ClassName IN('StaffPage'), lastname LIKE '%".$data['LastName']."%' AND firstname LIKE '%".$data['FirstName']."%'");
return $this->customise(array('StaffSearchResults' => $results))->renderWith(array('StaffSearch_results','Page'));
}

I am away from my office today and despite putting other IPs and hostnames into developer sites in _config.php I can get verbose output only on localhost. Tomorrow I (hopefully) will get it

Victor

Avatar
Victor

Community Member, 128 Posts

25 March 2009 at 2:02am

OK I found an error: it should be AND instead of ,

function doStaffSearch($data, $form)
{
$results = DataObject::get("SiteTree","ClassName IN('FacultyPage','StudentPage','StaffPage') AND lastname LIKE '%".$data['FirstName']."%' AND firstname LIKE '%".$data['LastName']."%'");
return $this->customise(array('StaffSearchResults' => $results))->renderWith(array('StaffSearch_results','Page'));
}

Avatar
Victor

Community Member, 128 Posts

25 March 2009 at 3:15am

And when I thought everything is fine ... I am confused:

StudentPage.php and FacultyPage.php are almost identical with the difference that they declare

< class StudentPage extends Page {

> class FacultyPage extends Page {

< class StudentPage_Controller extends Page_Controller {
---
> class FacultyPage_Controller extends Page_Controller {

Then StudentHolder.php

class StudentHolder extends Page {
static $db = array(
);
static $has_one = array(
);

static $allowed_children = array('StudentPage');
}

class StudentHolder_Controller extends Page_Controller {

function StudentSearchForm()
{
return new Form(
$this,
"StudentSearchForm",
new FieldSet(new TextField('FirstName','First Name starts from'), new TextField('LastName','Last Name starts from')),
new FieldSet(new FormAction('doStudentSearch','Search'))
);
}

function doStudentSearch($data, $form)
{
$results = DataObject::get("SiteTree","ClassName IN('StudentPage') AND lastname LIKE '".$data['LastName']."%' AND firstname LIKE '".$data['FirstName']."%'");
return $this->customise(array('StudentSearchResults' => $results))->renderWith(array('StudentSearch_results','Page'));
}
}

?>

and FacultyHolder.php is a copy with Student replaced by Faculty everywhere.

Then

StudentSearch_results.ss

<div class="typography">
$StudentSearchForm
<% if StudentSearchResults %>
<ul>
<% control StudentSearchResults %>
<li><a href="$Link">$firstname $lastname</a></li>
<% end_control %>
</ul>
<% else %>
There were no results that matched your query.
<% end_if %>
</div>

and FacultySearch_results.ss is again just replacement Student to Faculty

Here is a strange part:

if I put either StudentPage.php, StudentHolder.php or FacultyPage.php, FacultyHolder.php into code and flush db, the corresponding search works perfectly.

if I put StudentPage.php, StudentHolder.php and FacultyPage.php, FacultyHolder.php into code and flush db both searches end up with SilverStripe error

The tables in db should be completely independent but the fields have the same names. May be this is the source of error?
However we have a lot of resons to have the same field names in both tables

Any suggestions? Thanks you in advance. Victor

Go to Top