julien-boudry / Condorcet

Command line application and PHP library, providing an election engine with a high-level interface. Native support 20+ voting methods, easy to extend. Support simple elections with ease or billions of votes in low resource environment. Intensively tested and highly polyvalent.
https://www.condorcet.io
MIT License
119 stars 11 forks source link

fetched votes from db and taking the vote #43

Closed phplicengine closed 3 years ago

phplicengine commented 3 years ago
Q A
Type Support
Concorcet version 2.x
PHP version 7.4
Installation Method Composer

I have this:

    use CondorcetPHP\Condorcet\Condorcet;
    use CondorcetPHP\Condorcet\Election;
    use CondorcetPHP\Condorcet\Candidate;
    use CondorcetPHP\Condorcet\CondorcetUtil;
    use CondorcetPHP\Condorcet\Vote;

    require_once ("./vendor/autoload.php");

    Condorcet::setDefaultMethod('Schulze'); 

    $election = new Election ();

   $votes = $em->getRepository('Entities\Vote')->findBy(['categoryId' => $_GET['id']]);
   foreach ($votes as $vote){
            $result[] = $vote->getVote();
            $vote1 = new Vote ( $vote->getVote() );
            $election->addVote($vote1); 
   }

 print_r($election->countVotes ());

print("<br />");
foreach ($election->getResult('Schulze') as $rank => $candidates) :

    echo 'Rank ' . $rank . ': ';
    echo implode(', ', $candidates);
    echo '<br />';

endforeach;

echo 'Schulze winner is : ' . $election->getWinner('Schulze')->getName() . '<br />';

//print_r($result);

This $vote->getVote() returns votes from db with this format: Brass Birmingham = Scythe > Concordia = Gaia Project = On Mars > Power Grid = Terraforming Mars > A Feast for Odin = Everdell = Spirit Island = Through the Ages > Terra Mystica = Wingspan > Gloomhaven

I get: Uncaught CondorcetPHP\Condorcet\Throwable\CondorcetException: [20]: You need to specify one or more candidates before voting (line: /home2/phplicen/public_html/shultze/vendor/julien-boudry/condorcet/lib/Election.php:320) thrown in vendor/julien-boudry/condorcet/lib/Election.php on line 320

Does this mean that I should define each individual candidate items again? why for getting the final result I should define individual candidates again?

julien-boudry commented 3 years ago

Yes, if you build a new Election object, you first need to add candidates to them before votes.